Class 14 Input File Streams
A program that reads 10 integers from a file int main() { string filename; // the name of the file to be opened ifstream fin; // the input file stream int array[10]; // an array to hold 10 integers cout << Enter file name << endl; cin >> filename; fin.open( filename.c_str() ); // unfortunately open will not take a string argument for ( int i = 0; i < 10; i++ ) // problem: if fin did not open, this will not do anything { fin >> array[i]; } }; fin.close();
When does fin.open fail? when the input file doesn t exist when you don t have read access to the file if file is already open by another program Test: if ( fin.fail() ) // this could also be if (!fin ) { cout << Could not open file << endl; }
Programming example Write a program that reads a file with 10 integers and writes them to another file The program should exit if either of the files fails to open
The input buffer When data is being read from a source, it is temporarily stored in an input buffer (array) This is true of cin as well as input file streams All input goes into this buffer, whether it is read in your program or not The input stream keeps track of the current position in the buffer. You can look at it with fin.peek() or cin.peek()
The extraction operator >> The extraction operator looks in the input buffer and 1) skips past white space (spaces, tabs, newline characters) 2) takes all characters that match the data type that is looking for 3) It stops when there is an inappropriate character for the data type If it can t extract any characters at all, the filestream fails. When it has failed, it will be unable to read data.
At what character does >> stop? character: the first whitespace after char (space, tab, newline) string: the first whitespace after string integer: the first non-digit after the int double/float: any character that isn t a digit or a decimal point (or if it reaches a second decimal point)
Example User inputs: 12 and then space, then 13, then tab, then 14, then enter twice Program: int numbers[3]; cin >> numbers[0] // line A >> numbers[1] // line B >> numbers[2]; // line C What happens in the input buffer?
Example: What happens in the input buffer? User enters: 123 123.4 123.45 Program: int ival; float fval; cin >> ival; cin >> ival; cin >> ival; cin >> fval; cin >> fval; // fails at this cin statement What happens: ival = 123 ival = 123 the next ival causes it to fail at the period
Fixing the input stream: clear If the input stream has failed, you can fix it by calling the clear function: cin.clear() or fin.clear() It is then necessary to get past the offending characters in the filestream.
Code: int cont; cout cin >> cont; Example: fix this code << Enter 1 to continue, 2 to stop << endl; User enters the character y and then enters a newline
Input stream function: ignore(int max, char delimiter) To move forward in the input stream, call cin.ignore( NUM, \n ); This will move past the first NUM characters, or to one character after the newline whichever happens first
Fixed code int cont; cout << please enter 1 to continue, 2 to stop cin >> cont; << endl; while ( cin.fail() ) { } cin.clear(); cin.ignore( 100, \n ); // ignores up to 100 characters cout << I did not understand your response: cin >> cont; << Please enter 1 to continue, 2 to stop << endl;
Example Write a program that gets integers from a file, input.dat It writes them to a second file, output.dat When: 1. a # is entered - this is written to output.dat 2. it fails to get an integer and there is no # it exits with a message