Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Text File I/O Chapter 6 Pages 262 - 274. 2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.

Similar presentations


Presentation on theme: "1 Text File I/O Chapter 6 Pages 262 - 274. 2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening."— Presentation transcript:

1 1 Text File I/O Chapter 6 Pages 262 - 274

2 2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening a file for output in C. The object holds the state information that is in the FILE struct in C. Call methods of the object to write the file.

3 Writing a Text File #include int main() { using namespace std; ofstream outfile; cout << "This program writes a short text file "; outfile.open("test.txt"); outfile << "Here is the first line of text\n"; outfile << "Here is the second line\n"; outfile << "Here is the third and final line\n"; outfile.close(); cout << "File test.txt written\n"; cin.get(); // Keep window open; return 0; }

4 4 Writing a Text File Where is it?

5 5 Writing a Text File When running from Visual Studio, the default directory is the project directory. Double click on file to open in Notepad.

6 6 Here is the File in Notepad

7 7 Get Filename from Keyboard We often want the user to specify the filename. Must be C String (array of char)

8 Get Filename from Keyboard #include int main() { using namespace std; char filename[500]; ofstream outfile; cout << "Name for output file? "; cin >> filename; outfile.open(filename); outfile << "Here is the first line of text\n"; outfile << "Here is the second line\n"; outfile << "Here is the third and final line\n"; outfile.close(); cout << "File " << filename << " written\n"; cin.get(); // Keep window open; cin.get(); // Need a second get() now. return 0; }

9 9 Program Running on Windows As before, the output file is in the project directory.

10 10 Reading a Text File #include Instantiate an ifstream object Call methods of the object to do input.

11 11 Reading a Text File #include #include // for file I/O #include // for exit() int main() { using namespace std; char filename[500]; ifstream infile; char input_line[500]; cout << "File input example\n"; cout << "Name of file to read: "; cin >> filename; cout << "Opening file " << filename << endl; infile.open(filename); Note that filename is a C String (char array)

12 12 Reading a Text File if (!infile.is_open()) { cout << "Failed to open file\n"; cin.get(); // Keep window open. cin.get(); return -1;// Error } // Input file is open while (infile.good()) { infile >> input_line; cout << "Next line is: " << input_line << endl; } Always check for success of fopen Read the file. Continue here when input fails (EOF or error)

13 13 Reading a Text File if (infile.eof()) { cout << endl << "End of file \n"; } else { cout << endl << "Error reading file\n"; } infile.close(); cout << "Normal termination\n"; cin.get(); // Keep window open. cin.get(); return 0; } Check cause of input failure Good! Bad!

14 14 Input Example Running

15 15 Observations >> for file input works just like it does for keyboard input Read the next word. Terminate input at whitespace. Probably not what was intended To read an entire line, use getline()

16 16 Chanages to Read Entire Line... // Input file is open while (infile.good()) { infile.getline(input_line, 500); cout << "Next line is: " << input_line << endl; }

17 17 File Input Example Running Now using getline() method

18 18 Reading Numbers from a File >> works as expected More or less So long as file format is what was expected.

19 19 Reading Numbers from a File // Input file is open while (infile.good()) { double value; infile >> value; cout << "Next input is: " << value << endl; } Change for numeric input

20 20 The Input File

21 21 Program read_file_numeric Running What is this?

22 22 Reading Numbers from a File // Input file is open while (infile.good()) { double value; infile >> value; cout << "Next input is: " << value << endl; } value is unchanged when EOF is reached.

23 23 Avoiding Bogus Input at EOF while (infile.good()) { double value; infile >> value; if (!infile.good()) { break; } cout << "Next input is: " << value << endl; } Check for error or EOF after attempting input and before using the result

24 24 Revised Program Running

25 25 Reading Integers from a Text File Integer Input File

26 26 Reading Integers from a Text File // Input file is open while (infile.good()) { int value; infile >> value; if (!infile.good()) { break; } cout << "Next input is: " << value << endl; } Only change

27 27 Integer File Input Example Running

28 28 What If Input Is Not As Expected?

29 29 Attempting to Read Bad Integer Input

30 30 Input Failure Invalid input puts the stream into the fail state. No further input will succeed unless we reset the state. Can hang in an endless loop if we just try to continue. Typically the best policy is to abort the program. Recovery is possible when it makes sense.

31 31 Summary File I/O in C++ is similar to console I/O Instantiate object to read or write. Call its methods to do I/O > work as with cout and cin. Check for EOF and errors on input Error handling is tricky!

32 32 Assignment Read about file I/O in the text Chapter 6, page 262 – 274 Try the examples from this presentation for yourself.


Download ppt "1 Text File I/O Chapter 6 Pages 262 - 274. 2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening."

Similar presentations


Ads by Google