Streams in C++

C++ Streams Library exports two stream objects – cin and cout.
<< is called the stream insertion operator. >> is the stream extraction operator.

C++ provides a <fstream> header, that exports the ifstream and ofstream types. Note: ifstream and ofstream are types, unlike cin and cout, which are objects.
To create an ifstream that reads from a file,
ifstream myStream (“myfile.txt”);
int myInteger;
myStream >> myInteger;
You can also open a file by using ifstream’s open function.
ifstream myStream;
myStream . open (“myfile.txt”);
After trying to open a file, you should check if the stream is valid –
ifstream myStream (“myfile.txt”);
if ( ! myStream . is_open())
cerr << “Couldn’t open file” << endl;

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: