1 / 32

Text File I/O

Text File I/O. Chapter 6 Pages 262 - 274. 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.

najwa
Download Presentation

Text File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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 <iostream> #include <fstream> 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. Writing a Text File Where is it?

  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. Here is the File in Notepad

  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 <iostream> #include <fstream> 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. Program Running on Windows As before, the output file is in the project directory.

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

  11. Reading a Text File #include <iostream> #include <fstream> // for file I/O #include <cstdlib> // 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. Reading a Text File Always check for success of fopen 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; } Read the file. Continue here when input fails (EOF or error)

  13. Reading a Text File Check cause of input failure 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; } Good! Bad!

  14. Input Example Running

  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. 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. File Input Example Running Now using getline() method

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

  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. The Input File

  21. Program read_file_numeric Running What is this?

  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. 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. Revised Program Running

  25. Reading Integers from a Text File Integer Input File

  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. Integer File Input Example Running

  28. What If Input Is Not As Expected?

  29. Attempting to Read Bad Integer Input

  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. 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 • << and >> work as with cout and cin. • Check for EOF and errors on input • Error handling is tricky!

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

More Related