InputOutput Objectives In this chapter you will Learn
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 get, ignore, fill, putback, and peek �Become familiar with file input and output �Become familiar with input failure 2
Input/Output Streams �I/O: sequence of bytes (stream of bytes) from source to destination �Bytes are usually characters, unless program requires other types of information �Stream: sequence of characters from source to destination �Input Stream: sequence of characters from an input device to the computer �Output Stream: sequence of characters from the computer to an output device 3
Standard I/O Devices �Use iostream to extract (receive) data from keyboard and send output to the screen �iostream contains definitions of two types �istream - input stream �ostream - output stream �iostream has two variables �cin - stands for common input �cout - stands for common output 4
Using cin and cout �To use cin and cout, the preprocessor directive #include <iostream> must be used �The declaration is similar to the following C++ statements: istream cin; ostream cout; �Input stream variables: type istream �Output stream variables: type ostream 5
cin and the Extraction Operator >> �The syntax of an input statement using cin and the extraction operator >> is cin >> variable. . . ; �The extraction operator >> is binary �The left-hand operand is an input stream variable such as cin �The right-hand operand is a variable of a simple data type 6
Standard Input �Every occurrence of >> extracts the next data item from the input stream �Two variables can be read using a single cin statement �No difference between a single cin with multiple variables and multiple cin statements with one variable �When scanning, >> skips all whitespace �Whitespace characters consist of blanks and certain nonprintable characters 7
Data Type of Input �>> distinguishes between character 2 and number 2 by the right hand operand of >> �If it is of type char, the 2 is treated as character 2 �If it is of the type int (or double) the 2 is treated as the number 2 8
9
Reading Data �When reading data into a char variable �Extraction operator >> skips leading whitespace, finds and stores only the next character �Reading stops after a single character 10
Reading Data (Continued) �To read data into an int or double variable �Extraction operator >> skips leading whitespace, reads plus or minus sign (if any), reads the digits (including decimal) �Reading stops on whitespace non-digit character 11
Example 3 -1 int a, b; double z; char ch, ch 1, ch 2; 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 1. cin >> z; 74. 35 z = 74. 35 2. cin >> z; 39 z = 39. 0 3. cin >> z >> a; 65. 78 38 z = 65. 78, a = 38 12
Statement Input Value Stored in Memory 4 60 a = 4, b = 60 8. cin >> a >> b; 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; 57 A 26. 9 a = 57, ch = 'A', z = 26. 9 13. cin >> z >> ch >> a; 36. 78 B 34 z = 36. 78, ch = 'B', a = 34 14. cin >> z >> ch >> a; 36. 78 z = 36. 78, ch = 'B', a = 34 15. cin >> a >> b >> z; 11 34 B 34 a = 11, b = 34, computer waits for the next number 13
Statement Input Value Stored in Memory 16. cin >> a >> z; 46 32. 4 68 a = 46, z = 32. 4, 68 is held for later input 17. cin >> a >> z; 78. 49 a = 78, z = 0. 49 18. cin >> ch >> a; 256 ch = '2', a = 56 19. cin >> a >> ch; 256 a = 256, computer waits for the input value for ch 20. cin >> ch 1 >> ch 2; AB ch 1 = 'A', ch 2 = 'B' 14
cin and the get Function �The get function �Inputs next character (including whitespace) �Stores character location indicated by its argument �The syntax of cin and the get function: cin. get(var. Char); var. Char �Is a char variable �Is the argument (parameter) of the function 15
Another Way to Read char Data The get() function can be used to read a single character. get() obtains the very next character from the input stream without skipping any leading whitespace characters 16
At keyboard you type: A[space]B[space]C[Enter] char first; char middle; char last; cin. get(first); cin. get(middle); cin. get(last); first ‘A’ first middle ‘’ middle last ‘B’ last NOTE: The file reading marker is left pointing to the space after the ‘B’ in the input stream 17
cin and the ignore Function �ignore: discards a portion of the input �The syntax to use the function ignore is: cin. ignore(int. Exp, ch. Exp); int. Exp is an integer expression ch. Exp is a char expression �If int. Exp is a value m, the statement says to ignore the next m characters or all characters until the character specified by ch. Exp 18
Use function ignore() to skip characters The ignore() function is used to skip(read and discard) characters in the input stream The call cin. ignore(how. Many, what. Char); will skip over up to how. Many characters or until what. Char has been read, whichever comes first 19
20
21
putback and peek Functions �putback function �Places previous character extracted by the get function from an input stream back to that stream �peek function �Returns next character from the input stream �Does not remove the character from that stream 22
putback and peek Functions (continued) �The syntax for putback: �istream. Var. putback(ch); �istream. Var - an input stream variable, such as cin �ch is a char variable �The syntax for peek: �ch = istream. Var. peek(); �istream. Var is an input stream variable (cin) �ch is a char variable 23
Dot Notation �In the statement cin. get(ch); cin and get are two separate identifiers separated by a dot �Dot separates the input stream variable name from the member, or function, name �In C++, dot is the member access operator 24
Disk Files for I/O #include <fstream> input data output data disk file executing program “my. Infile. dat” your variable (of type ifstream) “my. Out. dat” your variable (of type ofstream) 25
Disk I/O To use disk I/O �Access #include <fstream> �Choose valid identifiers for your filestreams and declare them �Open the files and associate them with disk names �Use your filestream identifiers in your I/O statements(using >> and << , manipulators, get, ignore) �Close the files 26
Disk I/O Statements #include <fstream> ifstream my. Infile; // Declarations ofstream my. Outfile; my. Infile. open(“my. In. dat”); // Open files my. Outfile. open(“my. Out. dat”); my. Infile. close(); my. Outfile. close(); // Close files 27
Opening a File Opening a file �Associates the C++ identifier for your file with the physical(disk) name for the file � If the input file does not exist on disk, open is not successful � If the output file does not exist on disk, a new file with that name is created � If the output file already exists, it is erased �Places a file reading marker at the very beginning of the file, pointing to the first character in the file 28
Stream Fail State �When a stream enters the fail state, �Further I/O operations using that stream have no effect at all �The computer does not automatically halt the program or give any error message �Possible reasons for entering fail state include �Invalid input data (often the wrong type) �Opening an input file that doesn’t exist �Opening an output file on a disk that is already full or is write-protected 29
Input Failure �Things can go wrong during execution �If input data does not match the corresponding variables, the program may run into problems �Trying to read a letter into an int or double variable would result in an input failure �If an error occurs when reading data �Input stream enters the fail state 30
Input Failure (continued) �Once in a fail state, all further I/O statements using that stream are ignored �The program continues to execute with whatever values are stored in variables �This causes incorrect results �The clear function restores input stream to a working state istream. Var. clear(); 31
Run Time File Name Entry #include <string> // Contains conversion function c_str ifstream in. File; string file. Name; cout << “Enter input file name: “ << endl; // Prompt cin >> file. Name; // Convert string file. Name to a C string type in. File. open(file. Name. c_str()); 32
Algorithm Main Module Level 0 Open files Get social security number Get name Write data in proper formats Close files Open Files Level 1 in. Data. open("name. dat") out. Data. open("name. out") 33
Get Name Get first name Get middle name or initial Get last name Write Data in Proper Formats Write first name, blank, middle name, blank, last name, blank, social security number Write last name, comma, first name, blank, middle name, blank, social security number Write last name, comma, blank, first name, blank, middle initial, period, blank, social security number Write first name, blank, middle initial, period, blank, last name 34
Middle initial Level 2 Set initial to middle. Name. substr(0, 1) + period Close files in. Data. close() out. Data. close() 35
C++ Program //******************************* // Format Names program // This program reads in a social security number, a first name // a middle name or initial, and a last name from file in. Data. // The name is written to file out. Data in three formats: // 1. First name, middle name, last name, and social security // number. // 2. last name, first name, middle name, and social // security number // 3. last name, first name, middle initial, and social // security number // 4. First name, middle initial, last name //******************************* 36
#include <fstream> #include <string> using namespace std; // Access ofstream // Access string int main() { // Declare and open files ifstream in. Data; ofstream out. Data; in. Data. open("name. dat"); out. Data. open("name. out"); // Declare variables string social. Num; // Social security number string first. Name; // First name string last. Name; // Last name string middle. Name; // Middle name string initial; // Middle initial 37
// Read in data from file in. Data >> social. Num >> first. Name >> middle. Name >> last. Name; // Access middle initial and append a period initial = middle. Name. substr(0, 1) + '. '; // Output information in required formats out. Data << first. Name << ' ' << middle. Name << ' ' << last. Name << ' ' << social. Num << endl; out. Data << last. Name << ", " << first. Name << ' ' << middle. Name << ' ' << social. Num << endl; out. Data << last. Name << ", " << first. Name << ' ' << initial << ' ' << social. Num << endl; out. Data << first. Name << ' ' << initial << ' ' << last. Name; // Close files in. Data. close(); out. Data. close(); return 0; } 38
- Slides: 38