Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

Similar presentations


Presentation on theme: "COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi."— Presentation transcript:

1 COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi

2 COMP102 Lab 072 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 COMP102 Lab 073 Stream Sequence of characters – user interactive  cin Input stream associated with keyboard  cout Output stream associated with display – file operation  ifstream Defined input stream associated with file  ofstream Defined output stream associated with file

4 COMP102 Lab 074 File Stream Before Getting data from a file Output data to a file 1. Declare a stream object  Creating a channel 2. Associate this stream object to the file  Connecting the stream component to the file with program

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

6 COMP102 Lab 076 Input File 1. Include the #include 2. Declare an input stream object ifstream fin; 3. Open an dedicated file fin.open(“filename.txt”); 4. Get character from file fin.get(character); 5. Close the file fin.close();

7 COMP102 Lab 077 Output File 1. Include the #include 2. Declare an output stream object ofstream fout; 3. Open an dedicated file fout.open(“filename.txt”); 4. Put character from file fout.put(character); 5. Close the file fout.close();

8 COMP102 Lab 078 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 COMP102 Lab 079 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 COMP102 Lab 0710 Stream Manipulators Used to manage the input and output format of a stream With the directive E.g. cout << endl; cout << hex; cout << setw(5);

11 COMP102 Lab 0711 Stream Manipulators #include setprecision(d)  Set number of significant digits to d setw(w)  Set field width to w setfill(c)  Set fill character to c setiosflags(f)  Set flag f to 1 resetiosflags(f)  Set flag f to 0

12 COMP102 Lab 0712 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 COMP102 Lab 0713 SUMMARY By the end of this lab, you should be able to: Read/Write/Manipulate file with directive  Formatting input and output stream with directive 


Download ppt "COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi."

Similar presentations


Ads by Google