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

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

Embed Size (px)

Citation preview

COMP102 Lab 07 1

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

COMP102 Lab 07 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

COMP102 Lab 07 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

COMP102 Lab 07 4

File Stream

Before Getting data from a file Output data to a file1. Declare a stream object

Creating a channel

2. Associate this stream object to the file Connecting the stream component to the

file with program

COMP102 Lab 07 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…

COMP102 Lab 07 6

Input File

1. Include the <fstream> #include <fstream>

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();

COMP102 Lab 07 7

Output File

1. Include the <fstream> #include <fstream>

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();

COMP102 Lab 07 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

COMP102 Lab 07 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();…

COMP102 Lab 07 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);

COMP102 Lab 07 11

Stream Manipulators

#include <iomanip> 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

COMP102 Lab 07 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;…

COMP102 Lab 07 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>