230 likes | 397 Views
Chapter 3 Input/Output. Objectives. Stream Read data from keyboard Predefined function Input stream functions Write data to screen Manipulators String I/O File I/O. I/O Streams (one-way one-lane). …This is 87 <br> 9.3 ABt%…. …5 4 Hello 98.3 $%^& $39.00…. Output stream objects.
E N D
Objectives • Stream • Read data from keyboard • Predefined function • Input stream functions • Write data to screen • Manipulators • String I/O • File I/O
I/O Streams (one-way one-lane) …This is 87 \n 9.3 AB\t%… …5 4 Hello 98.3 $%^& $39.00… Output streamobjects Input streamobjects …5 4 Hello 98.3 $%^& $39.00… …This is 87 \n 9.3 AB\t%…
cin and >> • >> skips all whitespace characters so it does not read blank • Input data must match the data type of variable • Redundant input will remain in the stream and be discarded after the program terminates • Input failure
Example 3-1 int a,b; double z; char ch,ch1,ch2; Statement Input Value Stored in Memory 1 cin>>ch; A ch='A' 2 cin>>ch; AB ch='A', 'B' is held for later input 3 cin>>a; 48 a=48 4 cin>>a; 46.35 a=46, .35 is held for later input 5 cin>>z; 74.35 z=74.35 6 cin>>z; 39 z=39.0 7 cin>>z>>a;65.78 38z=65.78, a=38 8 cin>>a>>b; 4 60 a=4, b=60 9 cin>>a>>ch>>z; 57 A 26.9 a=57, ch='A', z=26.9
10 cin>>a>>ch>>z; 57 A 26.9 a=57, ch='A', z=26.9 11 cin>>a>>ch>>z; 57 A 26.9 a=57, ch='A', z=26.9 12 cin>>a>>ch>>z; 57A26.9 a=57, ch='A', z=26.9 13 cin>>z>>ch>>a; 36.78B34 z=36.78, ch='B', a=34 14 cin>>z>>ch>>a; 36.78 B34 z=36.78, ch='B', a=34 15 cin>>a>>b>>z; 11 34 a=11, b=34, Computerwaits for the nextnumber
16 cin>>a>>z; 46 32.4 68 a=46, z=32.4, 68 is held for later input • 17 cin>>ch>>a; 256 ch='2', a=56 • 18 cin>>a>>ch; 256 a=256, computer waits for the input value for ch. • 19 cin>>ch1>>ch2; A B ch1 = 'A', ch2 = 'B’
Using Predefined Functions • A function is a set of codes that can accomplish something • C++ library contains many predefined functions that are organized into various header files • To use a predefined function, you must #include the header file into your program
Using Predefined Functions • To call (activate) a function, you need to specify • Name of the function • Number of parameters taken by the function • Type of each parameter • Need to know what the function does • Sometimes also need to know the return type of a function
cin.get() cin.get(charVariable); • Name: get()an identifier followed by () always refer to a function • # of parameter: one • Type of parameter: char variable • Get the first character in the current stream and assign it to charVariable (the character is removed from the stream)
cin.ignore() cin.ignore(intExp, charExp); • Name: ignore() • # of parameters: two • Type of 1st parameter: int expressionType of 2nd parameter: char expression • Ignore the next intExp characters or ignore the input until it encounters the character specified by charExp, whichever comes first (note charExp is removed from the current stream)
cin.putback() cin.putback(charVar); • Name: putback() • # of parameter: one • Type of parameter: char • Put a character specified by charVar back into the stream
cin.peek() cin.peek(); • Name: peek() • # of parameter: none • Type of parameter: none (void) • Return the next character in the input stream, but does not remove that character from the input stream • Return type: charThe returned value can be assigned to a variable or used in an expression
The Dot Notation in the Function Call • A dot must be used between cin and function names • The dot is member access operator • cin is an istream variable (object)get(), ignore(), putback(), peek() are member functions of istream object • “Human can walk, sing, dance …”walk(), sing(), dance() are member functions of human objectTom.walk(); Tom.sing(); Tom.dance();
Input Failure and cin.clear() • Mismatch between input data and corresponding variable causes input stream enter into input failure • All further input statements using that stream are ignored • Can use cin.clear() to return the input stream to a working state (Need to combine with calling cin.ignore() to clear up the stream)
Formatting Output • #include <iomanip> • cout << manipulator << …;
getline() getline(istreamVar, stringVar); or getline(istreamVar, stringVar, charDelimiter); • Stand-alone function, no dot notation • Can read from either keyboard or file depending on the type of istreamVar • Reads until the end of the line or the delimiter if specified, whichever comes first
File I/O • File: an area in secondary storage used to hold information • Plain text file ifstream: for input from file • fstream ofstream: for output to file • #include <fstream>
File I/O Steps • Declare file stream variables (objects) ifstream input; ofstream output; • Associate file stream variables with the source files input.open(“c:\\temp\\data.txt”); output.open(“c:\\temp\\out.txt”); Escape sequence
File I/O Steps • Use file stream variable with <<, >>, or other predefined functions to read and write data • input >> payRate; • output << totalPay << endl; • Close the files • input.close(); • output.close();
More on Opening Files open(fileName, fileOpeningMode);