1 / 26

Text File I/O

Text File I/O. Reading Data From and Writing Data To Text Files. Text File I/O. C++ Provides a File Stream library to read/write Text Files using syntax similar to cin / cout #include < fstream >. Text File I/O. Text files must be Opened before they can be

ksarratt
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 Reading Data From and Writing Data To Text Files

  2. Text File I/O C++ Provides a File Stream library to read/write Text Files using syntax similar to cin/cout #include <fstream>

  3. Text File I/O Text files must be Opened before they can be read from or written to, with a File Name specified. They should also be Closed after use.

  4. Text File I/O #include <fstream> – defines the following: ofstream– data type used to declare a File Handle (variable) used to write to a Text File. ifstream– data type used to declare a File Handle (variable) used to read from a Text File.

  5. ofstream Algorithm: To write data to a text file: - declare an ofstream file handle - (try to) open the file - if the file opened successfully, use the file handle exactly as cout is used. - close the file when done (forgetting this may result in lost data!)

  6. ofstream #include <fstream> void main() { ofstreamf; f.open(”mydata.txt”); if (!f.fail()) { f << ”Hello world ” << endl; f << setw(10) << 3.14159; f.close(); } }

  7. ofstream ofstream f; This declaration statement allocates a File Handle variable named f that can be used to write data to a file. Think of it as a “pipeline to a file”. Currently the “pipeline” is not connected to any file.

  8. ofstream f.open(”mydata.txt”); This statement tries to open a file called ”mydata.txt” It may fail or succeed. If it succeeds, it “connects the pipeline” to the file.

  9. ofstream f.open(”mydata.txt”); If the file does not already exist, it will create a new file. If the file does already exist: IT WILL ERASE THE CURRENT CONTENTS OF THE FILE!

  10. ofstream f.open(”mydata.txt”); Without a Path in the file name, it is assumed the file is in the Working Directory (Folder). Where is the Working Directory? It depends, usually: - the same folder as the executable (.exe) - when using the Debugger in MS-VS, in the same folder as the source (.cpp)

  11. ofstream f.open(”mydata.txt”); Optionally, the complete path may be included in the file name: f.open( ”c:\\datafiles\\joiner\\mydata.txt”);

  12. ofstream ”c:\\datafiles\\joiner\\mydata.txt” Why the double backslashes?!? Remember, for C++ string literals, a backslash is interpreted as “escape character coming up next”, and “\\” is interpreted as a SINGLE backslash. So the above C++ string literal actually means: c:\datafiles\joiner\mydata.txt

  13. ofstream A string variable or constant may be used for the file name. string fn= ”c:\\files\\mydata.txt”; f.open(fn);

  14. ofstream A string variable can be used to get the file name from the user: string fname; cout << ”Enter the file name: ”; cin >> fname; f.open(fname); But the user should NOT use \\ for \ (That silly escape character stuff is only for Programmers)

  15. ofstream ofstream f( filename); As a “shortcut”, a File Handle may be declared and opened with one statement: ofstream f (”mydata.txt”); is the same as: ofstream f; f.open(”mydata.txt”);

  16. ofstream f.fail() It is possible that opening the file may fail for some reason (more likely with ifstream’s: see below). So the program should check every time .open() is used to ensure the file actually did open. The .fail() function returns true or false, and so may be used a conditional expression.

  17. ofstream f.fail() Example f.open(”mydata.txt”); if (f.fail()) { cout << ”Failed to open file.\n”; return; // or do whatever the specs say }

  18. ofstream f << Once the file is successfully open, use the File Handle the same as cout is used. Instead of printing on the screen, the text is written to the file. f << ”Fed Tax: ” << setw(10) << fedTax << endl;

  19. ofstream f.close(); Once all data is written to the file, it should be closed. Failure to do so may mean the last characters inserted into to the file handle may not actually be written to the file (data is lost).

  20. ofstream #include <fstream> void main() { ofstreamf; f.open(”mydata.txt”); if (!f.fail()) { f << ”Hello world ” << endl; f << setw(10) << 3.14159; f.close(); } }

  21. ifstream Use ifstreamas a Data Type to create a File Handle used to read data from a text file. Most syntax/issues of using ofstreams apply to using ifstreams. The main issue with ifstreams is not syntax/semantics, but is Algorithm Design

  22. ifstream #include <fstream> void main() { ifstream f; string name; int age; float gpa; f.open(”mydata.txt”); if (!f.fail()) { f >> name >> age >> gpa; f.close(); } }

  23. ifstream f.open() .open() for ifstreams has the same issues as for ofstreams, with one exception: If the file does not exist, the .open() fails.

  24. ifstream f >> Use the extraction operator on the file handle just as is used with cin. Differences with cin: - does NOT suspend the program to wait for the user (data is not coming from the keyboard!). - no prompts are needed (no human to ask!)

  25. Design of Reading Files The Algorithm for reading a text file is dictated by the Format/Layout of the File.

  26. Design of Reading Files See more in the in-class examples

More Related