extract number of entries in a line

L

levent

What is an elegant way (using std::stream's) to extract number of
white-space separated entries in a given line of a formatted text file?


e.g.: Take this section of a file,

1 2.78 4 5 -0.003 <tab> 7.1d5
9 40 <tab> 2.e5 -10
....

and the code will return 6 for 1st line, 4 for next line, etc...

- slyi
 
V

Victor Bazarov

levent said:
What is an elegant way (using std::stream's) to extract number of
white-space separated entries in a given line of a formatted text file?


e.g.: Take this section of a file,

1 2.78 4 5 -0.003 <tab> 7.1d5
9 40 <tab> 2.e5 -10
...

and the code will return 6 for 1st line, 4 for next line, etc...

Read the line using 'std::getline'.
Define 'std::eek:stringstream' from the string you just read.
Read fields as 'std::string' objects until the end of the string stream.

V
 
F

Frank Chang

Victor, Did you mean to say std::istringstream?

string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, ' ');
}
 
F

Frank Chang

Victor, I forgot to cut and paste the closing brace.

string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, ' ');
}
 
F

Frank Chang

Victor, You are right. You don't even need the iss.ignore:

string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
}
 
L

levent

Thx for the answers. They have some issues though:

- in order to extract the whole line into a string we need some sort of
prespecified (or pre-detected) limit for # of char's per line
(arguably, not elegant).
- the question is not to extract the entries themselves. It is to
extract the number of entries.

I meant something more compact such as:

ifstream file("thefile");
double tmp;
int nCol;
// effectively, the task takes just one line:
for(nCol=0; _pred_ ; nCol++) file >> tmp;

where _pred_ is such that it returns false when end of *line* is
reached, or smth like that.
 
P

Pete Becker

Frank said:
Victor, You are right. You don't even need the iss.ignore:

string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
}

while (iss >> word)
cout << word << '\n';

But even then, it doesn't solve the original problem.

int count = 0;
while (iss >> word)
++count;
cout << count << '\n';
 
F

Frank Chang

Yes, I know you wanted the count , not the entries themselves. I just
took the code from an application I wrote just to illustrate the
general idea.

Could you please tell me why this is true? As Victor said, std::getline
will handle a line of arbitrary length.
 
L

levent

that's right. I was confused with istream::getline( ).

the answer was actually trivial, as the general idea you were trying to
point out.

thanks all

- slyi
 
F

Frank Chang

slyi, Actually the std::istringstream class can handle more than
white-space character delimiters, as my earlier example attempted to
show:

istringstream iss(line);
while (iss)
{
iss >> word;
iss.ignore(1, ' '); // the delimiter does not have to be white space
}

The ignore member function takes two arguments, the first is the number
of characters to be extracted and ignored and the second is the
delimiter character. In effect, the class istringstream gives you an
elegant tokenizer DFA for free so that you don't have to write your own
tokenizer C++ class.
 
D

Default User

Frank said:
Yes, I know you wanted the count , not the entries themselves. I just
took the code from an application I wrote just to illustrate the
general idea.


It looks like you want to quote text using the broken Google interface,
but don't know how. To do so, DON'T use the Reply at the bottom of the
message. Click "show options" and use the Reply in the expanded header.
That will give you proper quotes and attributions.

This has been a public service announcement.



Brian
 
F

Frank Chang

This is a test.
Default said:
It looks like you want to quote text using the broken Google interface,
but don't know how. To do so, DON'T use the Reply at the bottom of the
message. Click "show options" and use the Reply in the expanded header.
That will give you proper quotes and attributions.

This has been a public service announcement.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top