16
Stream and Files Stream and Files Chapter: Chapter: 12 12 Lecture: 49 Lecture: 49 Date: 13.11.2012 Date: 13.11.2012

Lec 49 - stream-files

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lec 49 - stream-files

Stream and FilesStream and Files

Chapter: 12Chapter: 12

Lecture: 49Lecture: 49

Date: 13.11.2012Date: 13.11.2012

Page 2: Lec 49 - stream-files

ObjectivesObjectives Overview of stream classesOverview of stream classes

Showing how to perform file-related activities using Showing how to perform file-related activities using streams:streams: How to read and write data to files in a variety of waysHow to read and write data to files in a variety of ways How to handle files or I/O errorsHow to handle files or I/O errors How files and OOP are relatedHow files and OOP are related

Page 3: Lec 49 - stream-files

StreamsStreams Stream (flow of data)Stream (flow of data)

A transfer of information in the form of a sequence of bytesA transfer of information in the form of a sequence of bytes

In C++, a stream is represented by an object In C++, a stream is represented by an object of a particular class; e.g., of a particular class; e.g., cincin and and coutcout are are objects of objects of iostreamiostream class. class.

I/O Operations:I/O Operations: Input:Input: A stream that flows from an input device ( i.e.: A stream that flows from an input device ( i.e.:

keyboard, disk drive, network connection) to main memorykeyboard, disk drive, network connection) to main memory Output:Output: A stream that flows from main memory to an A stream that flows from main memory to an

output device ( i.e.: screen, printer, disk drive, network output device ( i.e.: screen, printer, disk drive, network connection)connection)

Page 4: Lec 49 - stream-files

MEM

CPU

HDD

keyboard

monitorterminalconsole

standard input

stream

standardoutput stream

StreamsStreams

What information What information travels across?travels across?

Page 5: Lec 49 - stream-files

MEM

CPU

HDD

keyboard

monitorterminalconsole

standard input

stream

standardoutput stream

fileinput strea

mLOADREAD file

output

stream

SAVEWRITE

StreamsStreams files

What information What information travels across?travels across?

Page 6: Lec 49 - stream-files

Str

eam

Cla

ss

Str

eam

Cla

ss

Hie

rarc

hy

Hie

rarc

hy

Page 7: Lec 49 - stream-files

What is a File?What is a File?

File:File: A file is a collection on information, A file is a collection on information, usually stored on a computer’s disk. usually stored on a computer’s disk. Information can be saved to files and then Information can be saved to files and then later reused.later reused.

File Names: File Names: All files are assigned a name All files are assigned a name that is used for identification purposes by that is used for identification purposes by the operating system and the user.the operating system and the user.

Page 8: Lec 49 - stream-files

File Names and File Names and ExtensionsExtensions

File Name and Extension

File Contents

MYPROG.BAS BASIC program

MENU.BAT DOS Batch File

INSTALL.DOC Documentation File

CRUNCH.EXE Executable File

BOB.HTML HTML (Hypertext Markup Language) File

3DMODEL.JAVA Java program or applet

INVENT.OBJ Object File

PROG1.PRJ Borland C++ Project File

ANSI.SYS System Device Driver

README.TXT Text File

Page 9: Lec 49 - stream-files

Writing to a FileWriting to a File

X

Z

Y

Page 10: Lec 49 - stream-files

Reading From a FileReading From a File

X

Z

Y

Page 11: Lec 49 - stream-files

Disk File I/O with Disk File I/O with StreamsStreams

Most programs need to save data to disk files Most programs need to save data to disk files and read it back in.and read it back in.

Working with disk files requires an other set of Working with disk files requires an other set of classes:classes: ifstreamifstream for (file) input for (file) input fstreamfstream for both (file) input and output for both (file) input and output ofstreamofstream for (file) output for (file) output

Objects of these classes can be associated Objects of these classes can be associated with disk files, and their member functions with disk files, and their member functions can be used to read and write to files.can be used to read and write to files.

Page 12: Lec 49 - stream-files

The Process of Using a The Process of Using a FileFile

Using a file in a program is a simple Using a file in a program is a simple three-step processthree-step process1)1) The file must be opened. If the file does not The file must be opened. If the file does not

yet exits, opening it means creating it.yet exits, opening it means creating it.

2)2) Information is then saved to the file, read Information is then saved to the file, read from the file, or both.from the file, or both.

3)3) When the program is finished using the file, When the program is finished using the file, the file must be closed.the file must be closed.

Page 13: Lec 49 - stream-files

#include <iostream>#include <fstream>#include <conio.h>using namespace std;

int main(){ char ch = ‘x’; int j = 77; double d = 6.02; string str1 = “Book”; //strings without embedded spaces string str2 = “Pen”; ofstream outfile(“fdata.txt”); //create ofstream objectoutfile << ch //insert (write) data << j << ‘ ‘ //needs space between numbers << d << str1 << ‘ ‘ //needs spaces between strings << str2;cout << “File written\n”;getch(); return 0; }

Writing to Writing to FileFile

Page 14: Lec 49 - stream-files

#include <iostream>#include <fstream>#include <conio.h>using namespace std;

int main(){ char ch; int j; double d; string str1; string str2;ifstream infile("fdata.txt"); //create ifstream object

//extract (read) data from itinfile >> ch >> j >> d >> str1 >> str2;

cout << ch << endl //display the data << j << endl << d << endl << str1 << endl << str2 << endl; getch(); return 0; }

Reading from Reading from FileFile

Page 15: Lec 49 - stream-files

#include <iostream>#include <fstream>#include <conio.h>using namespace std;

int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; getch(); return 0; }

Writing to File (easy Writing to File (easy way)way)

Page 16: Lec 49 - stream-files

#include <iostream>#include <fstream>#include <conio.h>using namespace std;

int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile, line); //extract characters into object myfile until line cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; getch(); return 0; }

Reading from File Reading from File (easy way)(easy way)