Hi Daniweb,

I'm looking to read in a file, but "ignore" the label prefixed to data.

For instance, if I have the following on one line, I want to ignore "Cities:" but read in the rest.

Cities: Chicago, New York, Atlanta

If someone can point me in the right way, thanks! Otherwise, no biggie.

Recommended Answers

All 5 Replies

In programming, there is no "right way" to do most things. There's this way, and that way, and the other way. The one you choose depends on what you know and what you are doing.

In this case, after you open the file, you can use file >> label to read the label followed by some form of getline() to read the rest of the line.

Assuming there are other lines in the file that have other tags at the beginning of the line, I would use getline() to read the entire line, then use stringstream to split it up into words. If you just use >> operator you won't know the difference between lines -- getline() will give you that control.

Assuming there are other lines in the file that have other tags at the beginning of the line, I would use getline() to read the entire line, then use stringstream to split it up into words. If you just use >> operator you won't know the difference between lines -- getline() will give you that control.

Unless you use getline() in conjunction with the >> operator as suggested.

The problem with >> is that it doesn't stop at the end of the line. If all you want to do is read just won't on a line you can do anything like while( infile >> word)

you could also use the ignore() function if a colon is always going to be attached to the end of your label. This will move your stream pointer one char past your colon. Then you can use getline() and it will get the rest of the line up to '\n' which would be your Chicago, New York, Atlanta.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.