Sei sulla pagina 1di 3

OBJECT: TO

BECOME FAMILIAR WITH FILES AND STREAMS.

Files and streams Generally file is the unit to store the data and the term stream refers to the flow of data, the flow of data can be in any direction. In C++ a stream is represented by an object of a particular class. Different streams are used to represent different kinds of data flow. For example, the ifstream class represents data flow from input disk files. In order to take the advantage of making the data safe we place the data into the files and we can also read those files at any time. To work with disk files we require a set of class: ifstream for input, ofstream for output and fstream for both the input and output. The objects of these classes can be associated with disk files, and we can use their member functions to read and write to the files.

ifstream is derived from istream and ofstream is derived from ostream, where as fstream is derived from iostream. The declaration of all the ifstream, ofstream and fstream classes are in the fstream.h header file.

File Input and Output Streams provide a uniform way of dealing with data coming from the keyboard or the hard disk and going out to the screen or hard disk. In either case, you can use the insertion and extraction operators or the other related functions and manipulators. To open and close files, you create ifstream and ofstream objects.

ofstream The particular objects used to read from or write to files are called ofstream objects. These are derived from the iostream objects. To get started with writing to a file, you must first create an ofstream object, and then associate that object with a particular file on your disk. To use ofstream objects, you must be sure to include fstream.h in your program. NOTE: Because fstream.h includes iostream.h, there is no need for you to include iostream explicitly.

Writing Data to the file

Program (stream1.cpp)
#include #include #include #include <iostream.h> <fstream.h> <string.h> <conio.h>

void main() { char ch=x; int j=77; double d=6.02; string str1=My; string str2=File; ofstream outfile(fdata.txt); outfile<<ch <<j << <<d<<str1<< <<str2; cout<<File Written; getch(); }
In the above program the outfilethe object of class ofstream is created and at the same time it is initialized fdata.txt file. The outfile object here will work same as the

cout does so we can use insertion operator << to output the variables to the file fdata.txt.
Here the notable point is that you should separate the numbers such as 77 and 6.02 with the nonnumber characters so that when we data is read from the same file the insertion operators should come to know that here one number starts and here it stops and the next begins. Also the strings should also be separated by a single space character for the same reason.

Reading Data from the file

Program (stream2.cpp)
#include #include #include #include <iostream.h> <fstream.h> <string.h> <conio.h>

void main() { char ch; int j; double d; string str1; string str2; ifstream infile(fdata.txt); outfile>>ch>>j>>d>>str1>>str2; cout<<ch<<endl <<j<<endl <<d<<endl <<str1<<endl <<str2<<endl; getch(); }
In the above program the object of class ifstream infile is created. The outfile object here will work same as the cin does so we can use extraction operator >> to read the variables from the file fdata.txt. the output of the program must be as:

x 77 6.02 My File

Potrebbero piacerti anche