1 / 13

COMP 102 Programming Fundamentals I

COMP 102 Programming Fundamentals I. Presented by : Timture Choi. File I/O. File External collection of data Provided input to a program Stored output from a program Stored on secondary storage permanently Must be Opened before used Closed after used File I/O

zhen
Download Presentation

COMP 102 Programming Fundamentals I

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. COMP 102Programming Fundamentals I Presented by : Timture Choi

  2. File I/O • File • External collection of data • Provided input to a program • Stored output from a program • Stored on secondary storage permanently • Must be • Opened before used • Closed after used • File I/O • Operation related to file

  3. Stream • Sequence of characters • <iostream> – user interactive • cin • Input stream associated with keyboard • cout • Output stream associated with display • <fstream> – file operation • ifstream • Defined input stream associated with file • ofstream • Defined output stream associated with file

  4. File Stream • Before • Getting data from a file • Output data to a file • Declare a stream object • Creating a channel • Associate this stream object to the file • Connecting the stream component to the file with program

  5. File Stream • E.g. #include <fstream> … ifstream fin; … fin.open("filename.txt"); // connects stream fsIn to the external // file "filename.txt" … fin.close(); // disconnects the stream and associated file …

  6. Input File • Include the <fstream> • #include <fstream> • Declare an input stream object • ifstream fin; • Open an dedicated file • fin.open(“filename.txt”); • Get character from file • fin.get(character); • Close the file • fin.close();

  7. Output File • Include the <fstream> • #include <fstream> • Declare an output stream object • ofstream fout; • Open an dedicated file • fout.open(“filename.txt”); • Put character from file • fout.put(character); • Close the file • fout.close();

  8. File I/O – Function • Input file • fin.eof() • Test for end-of-file condition • fin.get(char& character) • Obtains the next character from the file stream • Put into the variable character • Output file • fout.put(char character) • Inserts character to the output file stream

  9. File I/O: More Function • E.g. … ifstream fin; char character; fin.open(“data.txt”); // open the file fin.get(character); // get the first char while(!fin.eof()){ // loop to read each character … fin.get(character); … } fin.close(); …

  10. Stream Manipulators • Used to manage the input and output format of a stream • With the directive <iomanip> • E.g. • cout << endl; • cout << hex; • cout << setw(5);

  11. Stream Manipulators • #include <iomanip> • setprecision(d) • Set number of significant digits tod • setw(w) • Set field width tow • setfill(c) • Set fill character toc • setiosflags(f) • Set flagfto 1 • resetiosflags(f) • Set flagfto 0

  12. Stream Manipulators • E.g. … int a = 1234567; int b = 10; … cout << hex << a << endl; … cout << setiosflags(ios::showpos) << b << endl; cout << resetiosflags(ios::showpos); … cout << setiosflags(ios::scientific) << b << endl; …

  13. SUMMARY • By the end of this lab, you should be able to: • Read/Write/Manipulate file with directive • <fstream> • Formatting input and output stream with directive • <iomanip>

More Related