Connecting Tech Pros Worldwide Forums | Help | Site Map

extract number of entries in a line

levent
Guest
 
Posts: n/a
#1: Aug 18 '05
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 18 '05

re: extract number of entries in a line


levent wrote:[color=blue]
> 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...[/color]

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

V
Frank Chang
Guest
 
Posts: n/a
#3: Aug 19 '05

re: extract number of entries in a line


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

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

Frank Chang
Guest
 
Posts: n/a
#4: Aug 19 '05

re: extract number of entries in a line


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, ' ');
}

levent
Guest
 
Posts: n/a
#5: Aug 19 '05

re: extract number of entries in a line


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.

Frank Chang
Guest
 
Posts: n/a
#6: Aug 19 '05

re: extract number of entries in a line


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;
}

Pete Becker
Guest
 
Posts: n/a
#7: Aug 19 '05

re: extract number of entries in a line


Frank Chang wrote:[color=blue]
> 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;
> }
>[/color]

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';

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Frank Chang
Guest
 
Posts: n/a
#8: Aug 19 '05

re: extract number of entries in a line


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.
[color=blue][color=green]
>>- 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).[/color][/color]

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

levent
Guest
 
Posts: n/a
#9: Aug 19 '05

re: extract number of entries in a line


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

Frank Chang
Guest
 
Posts: n/a
#10: Aug 19 '05

re: extract number of entries in a line


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.

Default User
Guest
 
Posts: n/a
#11: Aug 19 '05

re: extract number of entries in a line


Frank Chang wrote:
[color=blue]
> 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.
>[color=green][color=darkred]
> > > - 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).[/color][/color][/color]


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
Frank Chang
Guest
 
Posts: n/a
#12: Aug 19 '05

re: extract number of entries in a line


This is a test.
Default User wrote:[color=blue]
> Frank Chang wrote:
>[color=green]
> > 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.
> >[color=darkred]
> > > > - 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).[/color][/color]
>
>
> 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[/color]

Default User
Guest
 
Posts: n/a
#13: Aug 19 '05

re: extract number of entries in a line


Frank Chang wrote:
[color=blue]
> This is a test.
> Default User wrote:[color=green]
> > Frank Chang wrote:[/color][/color]
[color=blue][color=green]
> > This has been a public service announcement.[/color][/color]

Ok, but don't top-post when doing it for real.



Brian

Closed Thread