1 / 61

C++ Files: Introduction and Usage

Learn about files in C++, including opening, reading, and writing to files. Understand file naming conventions and file stream operations.

ballen
Download Presentation

C++ Files: Introduction and Usage

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. Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 11 & 12: C++ Files By Dr. Mohamed El-Ramly

  2. Lecture ObjectivesC++ Functions • 1- Quiz revision • 2- What is a file • 3- C++ file opening • 4- Reading from a file • 5- Writing to a file

  3. I. Quiz Revision • float num = 14.50; • cout << num / 7 << " “; • cout << num % 7; • float num = 14.50; • cout << num / 2 << " “; • cout << (int) num % 7;  √

  4. I. Quiz Revision int n = 1; switch (n) { case 0: n = 5; case 1: n = 15; default: n = 25; } cout << n; Missing break;

  5. I. Quiz Revision • int x = 10; • cout << ((x++ * 3 + 7) / 2 * 2.5/2); Integer Division => 37 / 2 = 18

  6. I. Quiz Revision • cout << true + 'A‘; • cout << sizeof (true + 'A'); • cout << (char) (true + 'A'); • cout << sizeof ((bool)(true + 'A')); int addition 66 Size of int 4 Char of 1 + 65 B Size of bool 1

  7. I. Quiz Revision • cout << trunc(2.6) << endl; • cout << floor(2.6) << endl; • cout << round(2.6) << endl; • cout << trunc(-3.3) << endl; • cout << floor(-3.3) << endl; • cout << round(-3.3) << endl; 2 2 3 -3 -4 -3

  8. I. Quiz Revision • uint16_t x = 1; • cout << x << 4 << " "; • cout << (x << 4)<< endl; 1416

  9. I. Quiz Revision & by Reference & by Value 100 still 100 void add1 (int& n){n = n + 100;} void add2 (int num){n = n + 50;} int main () { int num = 0; add1 (num); cout << num << " "; add2 (num); cout << num << " "; }

  10. I. Quiz Revision

  11. I. Quiz Revision

  12. 2. What is a File? • A file is a collection on data, usually stored on a computer’s disk. • It is not reasonable to reenter the needed data every time we want to use it. • Data can be saved to files and then later reused.

  13. 2. What is a File?

  14. File Names • All files are assigned a name that is used for identification purposes by the operating system and the user. • Usually a file name has an extension to identify the format (type of data) stored in it. • OS associates specific programs with file extension.

  15. File Names • List of popular Windows extensions https://it.nmu.edu/docs/common-windows-file-extensions • abc.cpp C++ source file • abc.exe Executable application • abc.o C++ object file • abc.java Java source file • abc.txt Text file

  16. Using Files • Open file • Read data from file • Write data to file • Append • Override • Close file

  17. Using Files

  18. Using Files

  19. 3. C++ Files • C++ allows file access through streams • A stream is a channel for sending or receiving data from outside the program. • cin is an input stream (istream) for getting data from the keyboard. • cout is an output stream (ostream) for sending data to the screen.

  20. C++ File Open • #include <fstream> • ifstream inFile; • inFile.open("customer.dat"); • // ifstream is “read-onl” file

  21. C++ File Open • ifstreaminput("input_file.txt"); ofstreamoutput("output_file.txt"); • fstream// file for input and output input("input_file.txt",ios::in); • fstream output("output_file.txt",ios::out); • fstream output("file.txt",ios::out|ios::in);

  22. C++ File Open • ifstream • ofstream • fstream

  23. Example #1 // Program shows the declaration of an fstream // object and the opening of a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char fileName[81]; cout << "Enter the name of a file: "; cin.getline(fileName, 81); dataFile.open(fileName, ios::out); cout << “File " << fileName << " opened.\n"; }

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

  25. Testing for Open Errors dataFile.open("cust.dat", ios::in); if (dataFile.fail()) { cout << "Error opening file.\n"; }

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

  27. Example #2 int main() { fstream dataFile; char fileName[81]; cout << "Enter the name of a file: "; cin.getline(fileName, 81); dataFile.open(fileName, ios::out); if (dataFile.fail()) { cout << "File open error!" << endl; return; } cout << "File " << fileName << " opened.\n"; cout << "Now closing the file.\n"; dataFile.close(); }

  28. 4. Reading from Files Example #3 int main( ) { fstream dataFile; char name[81]; dataFile.open("demofile.txt", ios::in); if (dataFile.fail()) cout << "File open error!" << endl; else { cout << "File opened successful.\n"; cout << "Now reading information.\n"; for (int i = 0; i < 4; i++) { dataFile >> name; cout << name << endl; } dataFile.close(); cout << "\nDone.\n"; } }

  29. Reading till End of File • Each file end is marked by an EOF integer value. • EOF is system-defined. • cout << EOF; // Prints -1 • The eof()member function reports when the end of a file has been encountered. if (inFile.eof()) inFile.close();

  30. Example #4 int main( ) { ....... dataFile >> name; // Read 1st name while (!dataFile.eof()) { cout << name << endl; dataFile >> name; } dataFile.close(); cout << "\nDone.\n"; }

  31. 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.

  32. Avoiding Reading Failure • Some times the file has wrong data type. while (!dataFile.eof() && !dataFile.fail()) { dataFile >> data; cout << data << endl; } _____________________________________________________ while (dataFile >> data) cout << data << endl;

  33. Testing File Errors • All stream objects have error state bits that indicate the condition of the stream.

  34. Testing File Errors

  35. Testing File Errors

  36. Example #5 void showState(fstream &file) { cout << "File Status:\n"; cout << " eof bit: " << file.eof() << endl; cout << " fail bit: " << file.fail()<< endl; cout << " bad bit: " << file.bad() << endl; cout << " good bit: " << file.good() << endl; file.clear(); // Clear any bad bits }

  37. Functions for Reading • File stream objects have member functions for more specialized file reading and writing.

  38. Example #6 // 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 nameFile; char input[81]; nameFile.open("murphy.txt", ios::in); if (!nameFile) { cout << "File open error!" << endl; return; }

  39. nameFile >> input; while (!nameFile.eof()) { cout << input; nameFile >> input; } nameFile.close(); }

  40. Sample File to Read • Each string is read seperately.

  41. Program Screen Output JayneMurphy47JonesCircleAlmond,NC28702

  42. The getline Member Function • dataFile.getline(charArray, 81, ‘\n’); charArray – This is the name of a character array, or a pointer to a section of memory. The information read from the file will be stored here. 81 – This number is one greater than the maximum number of characters to be read. In this example, a maximum of 80 characters will be read. ‘\n’ – This is a delimiter character of your choice. If this delimiter is encountered, it will cause the function to stop reading before it has read the maximum number of characters. (This argument is optional. If it’s left our, ‘\n’ is the default.)

  43. Example #7 // This program uses the file stream object's // getline member function to read a line of // information from the file. #include <iostream.h> #include <fstream.h> void main(void) { fstream nameFile; char input[81]; nameFile.open("murphy.txt", ios::in); if (!nameFile) { cout << "File open error!" << endl; return; }

  44. nameFile.getline(input, 81); // use \n as a delimiter while (!nameFile.eof()) { cout << input << endl; nameFile.getline(input, 81); // use \n as a delimiter } nameFile.close(); }

More Related