Connecting Tech Pros Worldwide Help | Site Map

extract number of entries in a line

  #1  
Old August 18th, 2005, 09:25 PM
levent
Guest
 
Posts: n/a
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

  #2  
Old August 18th, 2005, 09:55 PM
Victor Bazarov
Guest
 
Posts: n/a

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
  #3  
Old August 19th, 2005, 12:35 AM
Frank Chang
Guest
 
Posts: n/a

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

  #4  
Old August 19th, 2005, 12:45 AM
Frank Chang
Guest
 
Posts: n/a

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

  #5  
Old August 19th, 2005, 12:55 AM
levent
Guest
 
Posts: n/a

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.

  #6  
Old August 19th, 2005, 12:55 AM
Frank Chang
Guest
 
Posts: n/a

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

  #7  
Old August 19th, 2005, 01:15 AM
Pete Becker
Guest
 
Posts: n/a

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)
  #8  
Old August 19th, 2005, 01:25 AM
Frank Chang
Guest
 
Posts: n/a

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.

  #9  
Old August 19th, 2005, 05:35 AM
levent
Guest
 
Posts: n/a

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

  #10  
Old August 19th, 2005, 12:55 PM
Frank Chang
Guest
 
Posts: n/a

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.

  #11  
Old August 19th, 2005, 05:45 PM
Default User
Guest
 
Posts: n/a

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
  #12  
Old August 19th, 2005, 05:55 PM
Frank Chang
Guest
 
Posts: n/a

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]

  #13  
Old August 19th, 2005, 08:25 PM
Default User
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting data from a file Amma answers 6 May 15th, 2007 11:30 AM
do professional PHP programmers use error checking in their code? lawrence k answers 16 September 4th, 2006 02:05 AM
Why must I use PrtDevMode Randy Harris answers 5 November 12th, 2005 04:11 PM
no database query is allowed yet value is still returned lawrence answers 2 July 16th, 2005 11:44 PM