Home

Extracting with Operators in C++

|
Updated:  
2016-03-26 08:30:12
|
From The Book:  
C++ Essentials For Dummies
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:

100
50
30
25

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
                     30
    25

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 Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jeff Cogswell has been an application developer and trainer for 18 years, working with clients from startups to Fortune 500 companies. He has developed courses on C++ and other technologies.