1 / 34

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++. Prof. Amr Goneid AUC Part 9. Streams & Files. Streams & Files. Streams & Files. What are Streams and Files? Standard I/O Streams I/O Manipulators External Files Structure of Text Files Declaration Opening & Closing One-Character I/O

minya
Télécharger la présentation

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

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. CSCE 110PROGRAMMING FUNDAMENTALSWITH C++ Prof. AmrGoneid AUC Part 9. Streams & Files Prof. amr Goneid, AUC

  2. Streams & Files Prof. amr Goneid, AUC

  3. Streams & Files • What are Streams and Files? • Standard I/O Streams • I/O Manipulators • External Files • Structure of Text Files • Declaration • Opening & Closing • One-Character I/O • String & Data I/O • Passing Files as Parameters Prof. amr Goneid, AUC

  4. 1. What are Streams and Files? • A stream is a sequence of characters • A stream has no fixed size • A stream associates its sequence with an external device (e.g. keyboard, screen, HD etc) • A file on HD is associated with a stream Prof. amr Goneid, AUC

  5. Streams and Files FILES Device streams Memory Device Device Prof. amr Goneid, AUC

  6. Extraction Operator Extracts sequence of characters from input stream and converts them to internal representation, skips leading blanks >> Memory Input Device Prof. amr Goneid, AUC

  7. Insertion Operator Converts internal representation to a sequence of characters and inserts them into the output stream in the proper format << Output Device Memory Prof. amr Goneid, AUC

  8. 2. Default I/O Streams • cin and cout are stream objects defined in the library <iostream> • cin is associated with the default input device (keyboard) • Extraction operator (>>) with cin. • cout is associated with the default output device (screen) • Insertion operator (<<) with cout. Prof. amr Goneid, AUC

  9. Default Streams FILES keyboard input stream Memory variable x cin >> x; screen output stream cout << x; Prof. amr Goneid, AUC

  10. Default Streams Library #include <iostream> I/O of simple quantities: int x; string Line; cin >> x; cout << “Hello”; getline (cin , Line); getline (cin , Line , ‘ * ’) Prof. amr Goneid, AUC

  11. Default Streams Library • Member functions for cin and cout can be used to input/output one character at a time (including blanks and NWLN): char c; cin.get(c); // Extract next character to c // Returns 0 in case of EOF cin.eof() // Test for EOF (CTRL-Z) cout.put(c) // Insert contents of c to screen Prof. amr Goneid, AUC

  12. Default Streams Library Example: char c; cin.get(c); // Extract character to c while ( ! cin.eof() ) // Test for EOF (CTRL-Z) { c = toupper (c); // Convert to uppercase cout.put(c) // Insert contents of c to screen cin.get(c); // Get next character } Prof. amr Goneid, AUC

  13. Example: CountChars.cpp // File: CountChars.cpp // Counts the number of characters and lines in // a file #include <iostream> #include <string> using namespace std; #define ENDFILE "CTRL-Z" Prof. amr Goneid, AUC

  14. CountChars.cpp int main() { const char NWLN = '\n'; // newline character char next; int charCount; int totalChars; int lineCount; lineCount = 0; totalChars = 0; Prof. amr Goneid, AUC

  15. CountChars.cpp cout << "Enter a line or press " << ENDFILE << ": "; while (cin.get(next)) { charCount = 0; while (next != NWLN && !cin.eof()) { cout.put(next); charCount++; totalChars++; cin.get(next); } // end inner while Prof. amr Goneid, AUC

  16. CountChars.cpp cout.put(NWLN); lineCount++; cout << "Number of characters in line " << lineCount << " is " << charCount << endl; cout << "Enter a line or press " << ENDFILE << ": "; } // end outer while cout << endl << endl << "Number of lines processed is " << lineCount << endl; Prof. amr Goneid, AUC

  17. CountChars.cpp cout << "Total number of characters is " << totalChars << endl; return 0; } Prof. amr Goneid, AUC

  18. 3. I/O Manipulators • #include <iomanip> when using • setw (int n) and width(int n) • setprecision (int n) • boolalpha • fixed & scientific • left & right Prof. amr Goneid, AUC

  19. 4. External Files • External stream objects • Used to read from or write to. • Elements with a File Pointer (FP) and an EOF. EOF FP Prof. amr Goneid, AUC

  20. 5. Structure of Text Files • Elements are single character or strings with EOLN • Access is Sequential • Opened in a Single Mode (input or output but not both). Mode can be changed after closing the file. FP character EOF EOLN EOF FP Prof. amr Goneid, AUC

  21. 6. Declaration • Use #include <fstream> for external text file stream objects. It provides two data types: ifstream for input files, ofstraem for output files • To declare an input file stream: ifstream <internal name>; e.g. ifstream source; • To declare an output file stream: ofstream <internal name>; e.g. ofstream target; • C++ uses internal name for I/O operations Prof. amr Goneid, AUC

  22. Declaration • A file has a DOS name that can be saved in a string, e.g. #define dosname “c:\data.txt” • Or read it from the keyboard into a string: e.g. string dosname; cin >> dosname; then use it in c-style , i.e. dosname.c_str() Prof. amr Goneid, AUC

  23. 7. Opening and Closing • Open for input only: source.open(dosname); // if we used #define // or source.open(dosname.c_str()); // if we read dosname as a string • Resets FP to the beginning. • source can be associated with other input files if we change the dosname. Prof. amr Goneid, AUC

  24. Opening and Closing • Open for output only: target.open(dosname); // if we used #define // or target.open(dosname.c_str()); // if we read dosname as a string • Creates a new file for writing or erases an already existing file. Resets FP to the beginning. • target can be associated with other input files if we change the dosname. Prof. amr Goneid, AUC

  25. Opening and Closing give non-zero values if file fails to open • To close opened files: source.fail() or target.fail() source.close(); target.close(); Prof. amr Goneid, AUC

  26. Example /* Declare two streams, one for input, the other for output. Attach them to two physical files, e.g. “data1.txt” and “data2.txt” */ #include <iostream> #include <fstream> string infile , outfile; ifstream source; ofstream target; cout << “Enter Input File Name: “; cin >> infile; cout << “Enter Output File Name: “; cin >> outfile; source.open ( infile.c_str() ); target.open (outfile.c_str() ); Prof. amr Goneid, AUC

  27. 8. One Character I/O • To read character by character: • Advances FP to next character • To test for EOF: char c; source.get(c); source.eof() Prof. amr Goneid, AUC

  28. One Character I/O • To write character by character: • Advances FP to next character location char c; target.put(c); Prof. amr Goneid, AUC

  29. 9. String & Data I/O // To read a whole line (including blanks) and // terminated by NWLN: string line; getline(source,line); // To read a string terminated by blank: source >> line; // in the style of cin >> line // To skip over next n characters: source.ignore(n); or source.ignore(n , ‘\n’); // To write a string followed by NWLN: target << line << endl; Prof. amr Goneid, AUC

  30. Data I/O • Numeric Data can also be written to and read from text files, e.g. int m = 20; int n = 300; float x = 1.345; // Write them separated by blanks target << m << “ “ << n << “ “ << x << “ “; // To read them: source >> m >> n >> x ; Prof. amr Goneid, AUC

  31. 10. Passing Files as Parameters • The declarations : ifstream source; ofstream target; declare source & target as “pointers” to the file stream objects. If they are parameters to or from functions, they should be passed by their address, e.g. int copyline (ifstream& source , ofstream& target); Prof. amr Goneid, AUC

  32. Passing Files as Parameters • Instead of passing streams, we may pass strings with the file names on disk and let the function do the opening and closing of the streams. • For example, a function to copy a file character by character to another file would have the header: void copychar ( string infile , string outfile) Prof. amr Goneid, AUC

  33. Function copychar /* Fuction to copy a file with a name stored in the string infile to another file with a name stored in string outfile (character by character) */ void copychar(string infile, string outfile) { char c; ifstream source; ofstream target; source.open(infile.c_str()); target.open(outfile.c_str()); source.get(c); while (! source.eof()) { target.put(c); source.get(c); } source.close(); target.close(); } Prof. amr Goneid, AUC

  34. A Main to call copychar /* A main function to drive copychar */ #include <iostream> #include <fstream> void copychar (string , string ); int main() { string infile , outfile; cout << “Enter Input File Name: “; cin >> infile; cout << “Enter Output File Name: “; cin >> outfile; copychar (infile,outfile); } Prof. amr Goneid, AUC

More Related