File Input and Output in C++

basic file i o and stream objects n.w
1 / 30
Embed
Share

Dive into the world of file input and output in C++. Learn about different kinds of files, creating file stream objects, and how to attach file streams to files, along with handling potential issues like file existence and permissions.

  • File Input
  • File Output
  • C++
  • Stream Objects
  • Programming

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Basic File I/O and Stream Objects Andy Wang Object Oriented Programming in C++ COP 3330

  2. Input and Output to and from Files File input and output is an essential in programming Most software involves more than keyboard input and screen user interfaces Data needs to be stored somewhere when a program is not running That means writing data to storage We need file input and file output techniques In C++, file IOs are similar to working with cin and cout

  3. Kinds of Files Formatted text vs. binary files A text file is made of readable text characters Looks like output printed to the screen through cout A binary file contains unformatted data Saved in its raw memory format E.g., the integer 123456789 is saved in a four-byte chunk

  4. Kinds of Files Sequential vs. Random access files A sequential file is written or read from start to finish A random access file stores records of the same size Can read or write single records in place without affecting the rest of the file This set of slides only deals with sequential text files

  5. Creating file stream objects cout and cin are objects cout is the standard output stream Represents the monitor Of the type ostream cin is the standard input stream Represents the keyboard Of the type istream

  6. To create file stream objects Include the <fstream> library #include <fstream> using namespace std; To declare file stream objects ofstream outputFile; ifstream inputFile;

  7. To attach file streams to files outputFile.open( outfile.txt ); inputFile.open( inputFile.txt ); Will open always work? A file may not exist You may not have the permission The directory may not be writable The file name may contain illegal characters

  8. To check a valid file is attached if (!inputFile) { // inputFile not attached to a valid file cout << Sorry, bad file. ; exit(0); // abort program // need to include <cstdlib> }

  9. To detach from the stream object inputFile.close(); outputFile.close(); The stream object can be attached to another file inputFile.open( inputFile2.txt );

  10. Using File Streams Once a file stream object is attached to a file, it can be used with the same syntax as cin and cout inputFile >> x >> y >> z; outputFile << Hello, World << endl;

  11. Output File Example http://www.cs.fsu.edu/~myers/c++/examples/filei o/set1/numout.cpp

  12. numout.cpp #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout; fout.open( datafile.txt ); if (!fout) { cerr << Attempt to create file failed\n ; exit(1); }

  13. numout.cpp int x, y, z; cout << Type in 3 integers: ; cin >> x >> y >> z; for (int j = 1; j <= 10; j++) { fout << x*j << \t ; fout << y*j << \t ; fout << z*j << \t ; } cout << All done!\n ;

  14. numout.cpp fout << endl; fout.close(); fout.open( logfile.txt ); if (!fout) { exit(0); } fout << Please disperse. Nothing to see here\n ; fout << Please move along.\n ; fout << Go away already!\n ; fout.close(); return 0; }

  15. Input File Example http://www.cs.fsu.edu/~myers/c++/examples/filei o/set1/numin.cpp

  16. numin.cpp #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; fin.open( datafile.txt ); if (!fin) { cerr << Attempt to open file failed\n ; exit(1); }

  17. numin.cpp int x, y, z; for (j = 1; j <= 10; j++) { fin >> x >> y >> z; cout << x << + << y << + << z << = << (x + y + z) << endl; } cout << All done!\n ; fin.close(); return 0; }

  18. Opening a file in append mode Default Open an output file and begin writing from the beginning Existing file can be opened for output New output is tacked to the end (appending) To open a file in append mode ofstream fout; Fout.open( file.txt , ios::app);

  19. Append Mode Example http://www.cs.fsu.edu/~myers/c++/examples/filei o/set1/numapp.cpp

  20. numapp.cpp #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout; fout.open( datafile.txt , ios::app); if (!fin) { cerr << Attempt to open file failed\n ; exit(1); }

  21. numapp.cpp int x, y, z; cout << Type in 3 integers: ; cin >> x >> y >> z; for (j = 0; j <= 10; j++) { fout << x*j << \t ; fout << y*j << \t ; fout << z*j << \t ; } cout << All done!\n ;

  22. numapp.cpp fout << endl; fout.close(); return 0; }

  23. User-entered file names File names don t have to be hard coded To store a file name, we can declare the following char filename[20]; // 19 char file name A user can enter a single word name into this variable cin >> filename; We can use this variable in the open() function ofstream fout; fout.open(filename);

  24. User Chosen Output File Example http://www.cs.fsu.edu/~myers/c++/examples/filei o/set1/userfile.cpp

  25. userfile.cpp #include <iostream> #include <fstream> using namespace std; int main() { char ofilename[25]]; ofstream out1; do { out1.clear(); //clear status flags cout << Name of the output file? ; cin >> ofilename;

  26. userfile.cpp out1.open(ofilename); if (!out1) cout << Not a valid file. Try again!\n ; } while (!out1); out1 << Hello, World\n ; out1.close(); cout << Processing complete\n ; return 1; }

  27. User Chosen IO File Example http://www.cs.fsu.edu/~myers/c++/examples/filei o/set1/userio.cpp

  28. userio.cpp #include <iostream> #include <fstream> using namespace std; int main() { char ofilename[25]]; ifstream in1; ofstream out1; do { in1.clear(); //clear status flags cout << Name of the input file? ; cin >> ofilename;

  29. userfile.cpp in1.open(ofilename); if (!in1) cout << Not valid. Try again!\n ; } while (!in1); do { out1.clear(); cout << Name of the output file? ; cin >> filename; out1.open(filename); if (!out1) cout << Not valid, try again!\n ; } while (!out1);

  30. userfile.cpp int x, y, z; in1 >> x >> y >> z; out1 << z << endl; out2 << y << endl; out2 << z << endl; in1.close(); out1.close(); cout << Processing complete\n ; return 1; }

More Related Content