comp 102 programming fundamentals i

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

Upload: zhen

Post on 18-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP 102 Programming Fundamentals I

COMP102 Lab 07 1

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

Page 2: COMP 102 Programming Fundamentals I

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

Page 3: COMP 102 Programming Fundamentals I

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

Page 4: COMP 102 Programming Fundamentals I

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

Page 5: COMP 102 Programming Fundamentals I

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…

Page 6: COMP 102 Programming Fundamentals I

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

Page 7: COMP 102 Programming Fundamentals I

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

Page 8: COMP 102 Programming Fundamentals I

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

Page 9: COMP 102 Programming Fundamentals I

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

Page 10: COMP 102 Programming Fundamentals I

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

Page 11: COMP 102 Programming Fundamentals I

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

Page 12: COMP 102 Programming Fundamentals I

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;…

Page 13: COMP 102 Programming Fundamentals I

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>