C++ All-in-One For Dummies
Book image
Explore Book Buy On Amazon

When you read from a file, you can use the extraction operator, >>. This operator is very easy to use, provided you recognize that the phrase, “Look mom, no caveats!” just doesn’t apply to the extraction operator.

Suppose you have a file called Numbers.txt with the following text on one line:

100 50 30 25

You can easily read in these numbers with the following code. First, make sure you add #include (but notfstream.h, as you’ll pick up an old, outdated, yucky file) as well as #include . And you probably will need the line using namespace std; if you’re using a newer compiler and library.

It’s important to define some variables to hold the data you want to read. These variables (found in the FileRead01 example) will work fine:

string weight;
string height;
string width;
string depth;

The variables define the statistics for some type of widget you want to build. After you have the variables in place, this code will do the work:

ifstream MyFile("Numbers.txt");
MyFile >> weight;
MyFile >> height;
MyFile >> width;
MyFile >> depth;

In the preceding code, the input file, Numbers.txt, had its numbers separated with spaces. You can also separate them with newline characters, like this:


The application doesn’t care. It looks for white space, which is any number of spaces, tabs, and newlines. You could format the data so it looks like the following example, and the application will still read them in correctly.

100        50

When you are dealing with the standard input object, cin, the same rules about white space apply: If you read in four numbers, like the following example, the cin object, like the ifstream object, will separate the numbers based on the white space.

cin >> weight;
cin >> height;
cin >> width;
cin >> depth;

If the user accidentally inserts a space, the computer will apply the separated values in two places — both incorrectly. Be careful!

When you are reading information from a file, make sure that you have clearly defined the order of the information. In other words, make sure that you have agreed upon a protocol for the information. Otherwise you’ll likely end up with errors and mistakes, and your coworkers will want to blame somebody. That’s the way computer people are, after all.

Of course, you’ll want to verify that the application actually works. Adding this code will do the trick:

cout << "Weight = " << weight << "rn";
cout << "Height = " << height << "rn";
cout << "Width  = " << width << "rn";
cout << "Depth  = " << depth;

When you run the application, you see the result of reading the file. Here is what you should see:

Weight = 100
Height = 50
Width  = 30
Depth  = 25

About This Article

This article is from the book:

About the book author:

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: