UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and <<overloaded operators >> and << put() and get() functionsput() and.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
1 Chapter 4 Program Input and the Software Design Process.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 8 Arrays and Strings
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Basic Input and Output Operations
getline() function with companion ignore()
Chapter 2 part #3 C++ Input / Output
Arrays and Strings Chapter 9.
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Managing console i/o operations
File I/O in C++ I.
Chapter 2 part #3 C++ Input / Output
Using string type variables
Input/Output Streams, Part 1
File I/O in C++ I.
Introduction to Programming
Presentation transcript:

UNFORMATTED INPUT OUTPUT

Topics to be discussed……………….. overloaded operators >> and <<overloaded operators >> and << put() and get() functionsput() and get() functions getline() and write() functionsgetline() and write() functions

Unformatted I/O Operations Overloaded Operators >> and << The objects cin and cout is used for input and output of data of various types. This has been made possible by overloading the operators >> and > operator is overloaded in the istream class and << is overloaded in the ostream class. The following is the general format for reading data from the keyboard: Cin>>variable1>>variable2>>……..>> variableN Variable1,variable2,….are valid C++ variable names that have been declared already

This statement will cause the computer to stop the execution and look for input data from the keyboard. The input for this statement would be: data1, data2……….dataN The operator >> reads the data character by character and assigns it to the indicated location. The reading for a variable will be terminated at the encounter of a white space or a character does not match the destination type. The general form for displaying data on the screen is: cout<<item1<<item2<<……………..<<itemN The items item1 through itemN may be variables or constants of any basic type

put() and get() functions The classes istream and ostream define two member functions get() and put() to handle the single character input/output operations. Example: char c; cin.get(c); // get a character from keyboard and assign it to c while(c!=‘\n’) { cout<<c; // display the character on screen cin.get(c) ; // get another character }

 The function put(), a number of ostream class, can be used to output a line of text, character by character. For Example: cout.put(‘x’) displays the character x and cout.put(ch) Displays the value of variable ch. The variable ch must contain a character value.  We can also use a number as an argument to the function put(). For example: cout.put(68); displays the character D. This statement will convert the int value 68 to a char value and display the character whose ASCII value is 68.

# include int main ( ) { int count = 0 char c; cout << “Input Text \n”; cin.get(c); while(c != ‘\n’) { cout.put(c); count++; cin.get(c); } cout << “\n Number of characters = “ << count << “\n”; }

getline() and write() functions We can read and display a line of text more efficiently using the line-oriented input/output functions getline() and write(). The getline() function reads a whole.The getline() function reads a whole line of text that ends with a newline character. This function can be invoked by using the object cin as follows: Cin.getline(line,size); The function call invokes the function getline() which reads character input into the variable line. The reading is terminated as soon as either the newline character ‘\n’ is encountered or size-1 characters are read(whichever occurs first).The newline character is read but not saved. Instead it is replaced by null character.

For example Char name[20]; Cin.getline(name,20); If we have given the following input through keyboard: Bjarne Stroustrup The input will read correctly and assigned to the character array name.If the input is as follows: Bjarne Stroustrup Programming In this case the output will be termiated after reading the following 19 characters: Bjarne Stroustrup Pro The two blank spaces contained in the string.are also taken into account

# include { int main() int size=20; char city[20]; cout << “Enter city name : \n”; cin >> city; cout << “City name : “ << city << “\n\n”; cout << “Enter city name again : \n”; cin.getline(city,size); cout << “City name now : “ << city << “\n\n”; cout << “Enter city name again : \n”; cin.getline(city,size); cout << “City name now : “ << city << “\n\n”; }

Write() The write function displays an entire line and has the following form: Cout.write(line,size) The first argument line represents the name of the string to be displayed and the second argument size indicates the number of characters to display.

THANKS