1 / 106

Chapter 12 – File Operations

Chapter 12 – File Operations. 12.1 What is a File?. A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused. 12.2 File Names.

tad
Download Presentation

Chapter 12 – File Operations

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. Chapter 12 – File Operations

  2. 12.1 What is a File? • A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.

  3. 12.2 File Names • All files are assigned a name that is used for identification purposes by the operating system and the user.

  4. Table 12-1

  5. 12.3 Focus on Software Engineering: The Process of Using a File • Using a file in a program is a simple three-step process • The file must be opened. If the file does not yet exits, opening it means creating it. • Information is then saved to the file, read from the file, or both. • When the program is finished using the file, the file must be closed.

  6. Figure 12-1

  7. Figure 12-2

  8. 12.4 Setting Up a Program for File Input/Output • Before file I/O can be performed, a C++ program must be set up properly. • File access requires the inclusion of fstream.h

  9. 12.5 Opening a File • Before data can be written to or read from a file, the file must be opened. ifstream inputFile; inputFile.open(“customer.dat”);

  10. Program 12-1 // This program demonstrates the declaration of an fstream // object and the opening of a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; // Declare file stream object char fileName[81]; cout << "Enter the name of a file you wish to open\n"; cout << "or create: "; cin.getline(fileName, 81); dataFile.open(fileName, ios::out); cout << "The file " << fileName << " was opened.\n"; }

  11. Program Output with Example Input Enter the name of a file you wish to open or create: mystuff.dat [Enter] The file mystuff.dat was opened.

  12. Table 12-3

  13. Table 12-4

  14. Table 12-4 continued

  15. Opening a File at Declaration fstream dataFile(“names.dat”, ios::in | ios::out);

  16. Program 12-2 // This program demonstrates the opening of a file at the // time the file stream object is declared. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile("names.dat", ios::in | ios::out); cout << "The file names.dat was opened.\n"; }

  17. Program Output with Example Input The file names.dat was opened.

  18. Testing for Open Errors dataFile.open(“cust.dat”, ios::in); if (!dataFile) { cout << “Error opening file.\n”; }

  19. Another way to Test for Open Errors dataFile.open(“cust.dat”, ios::in); if (dataFile.fail()) { cout << “Error opening file.\n”; }

  20. 12.6 Closing a File • A file should be closed when a program is finished using it.

  21. Program 12-3 // This program demonstrates the close function. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; dataFile.open("testfile.txt", ios::out); if (!dataFile) { cout << "File open error!" << endl; return; } cout << "File was created successfully.\n"; cout << "Now closing the file.\n"; dataFile.close(); }

  22. Program Output File was created successfully. Now closing the file.

  23. 12.7 Using << to Write Information to a File • The stream insertion operator (<<) may be used to write information to a file. outputFile << “I love C++ programming !”

  24. Program 12-4 // This program uses the << operator to write information to a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char line[81]; dataFile.open("demofile.txt", ios::out); if (!dataFile) { cout << "File open error!" << endl; return; }

  25. Program continues cout << "File opened successfully.\n"; cout << "Now writing information to the file.\n"; dataFile << "Jones\n"; dataFile << "Smith\n"; dataFile << "Willis\n"; dataFile << "Davis\n"; dataFile.close(); cout << "Done.\n"; }

  26. Program Screen Output File opened successfully. Now writing information to the file. Done. Output to File demofile.txt Jones Smith Willis Davis

  27. Figure 12-3

  28. Program 12-5 // This program writes information to a file, closes the file, // then reopens it and appends more information. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; dataFile.open("demofile.txt", ios::out); dataFile << "Jones\n"; dataFile << "Smith\n"; dataFile.close(); dataFile.open("demofile.txt", ios::app); dataFile << "Willis\n"; dataFile << "Davis\n"; dataFile.close(); }

  29. Output to File demofile.txt Jones Smith Willis Davis

  30. 12.8 File Output Formatting • File output may be formatted the same way as screen output.

  31. Program 12-6 // This program uses the precision member function of a // file stream object to format file output. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; float num = 123.456; dataFile.open("numfile.txt", ios::out); if (!dataFile) { cout << "File open error!" << endl; return; }

  32. Program continues dataFile << num << endl; dataFile.precision(5); dataFile << num << endl; dataFile.precision(4); dataFile << num << endl; dataFile.precision(3); dataFile << num << endl; }

  33. Contents of File numfile.txt 123.456 123.46 123.5 124

  34. Program 12-7 #include <iostream.h> #include <fstream.h> #include <iomanip.h> void main(void) { fstream outFile("table.txt", ios::out); int nums[3][3] = { 2897, 5, 837, 34, 7, 1623, 390, 3456, 12 }; // Write the three rows of numbers for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { outFile << setw(4) << nums[row][col] << " "; } outFile << endl; } outFile.close(); }

  35. Contents of File TABLE.TXT 2897 5 837 34 7 1623 390 3456 12

  36. Figure 12-6

  37. 12.9 Using >> to Read Information from a File • The stream extraction operator (>>) may be used to read information from a file.

  38. Program 12-8 // This program uses the >> operator to read information from a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char name[81]; dataFile.open("demofile.txt", ios::in); if (!dataFile) { cout << "File open error!" << endl; return; } cout << "File opened successfully.\n"; cout << "Now reading information from the file.\n\n";

  39. Program continues for (int count = 0; count < 4; count++) { dataFile >> name; cout << name << endl; } dataFile.close(); cout << "\nDone.\n"; }

  40. Program Screen Output File opened successfully. Now reading information from the file. Jones Smith Willis Davis Done.

  41. 12.10 Detecting the End of a File • The eof() member function reports when the end of a file has been encountered. if (inFile.eof()) inFile.close();

  42. Program 12-9 // This program uses the file stream object's eof() member // function to detect the end of the file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char name[81]; dataFile.open("demofile.txt", ios::in); if (!dataFile) { cout << "File open error!" << endl; return; } cout << "File opened successfully.\n"; cout << "Now reading information from the file.\n\n";

  43. Program continues dataFile >> name; // Read first name from the file while (!dataFile.eof()) { cout << name << endl; dataFile >> name; } dataFile.close(); cout << "\nDone.\n"; }

  44. Program Screen Output File opened successfully. Now reading information from the file. Jones Smith Willis Davis Done.

  45. Note on eof() • In C++, “end of file” doesn’t mean the program is at the last piece of information in the file, but beyond it. The eof() function returns true when there is no more information to be read.

  46. 12.11 Passing File Stream Objects to Functions • File stream objects may be passed by reference to functions. bool openFileIn(fstream &file, char name[51]) { bool status; file.open(name, ios::in); if (file.fail()) status = false; else status = true; return status; }

  47. 12.12 More Detailed Error Testing • All stream objects have error state bits that indicate the condition of the stream.

  48. Table 12-5

  49. Table 12-6

  50. Program 12-11 // This program demonstrates the return value of the stream // object error testing member functions. #include <iostream.h> #include <fstream.h> // Function prototype void showState(fstream &); void main(void) { fstream testFile("stuff.dat", ios::out); if (testFile.fail()) { cout << "cannot open the file.\n"; return; }

More Related