extract number of entries in a line

Discussion in 'C++' started by levent, Aug 18, 2005.

  1. levent

    levent Guest

    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
    levent, Aug 18, 2005
    #1
    1. Advertising

  2. levent wrote:
    > 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
    Victor Bazarov, Aug 18, 2005
    #2
    1. Advertising

  3. levent

    Frank Chang Guest

    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, Aug 19, 2005
    #3
  4. levent

    Frank Chang Guest

    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, ' ');
    }
    Frank Chang, Aug 19, 2005
    #4
  5. levent

    Frank Chang Guest

    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;
    }
    Frank Chang, Aug 19, 2005
    #5
  6. levent

    levent Guest

    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.
    levent, Aug 19, 2005
    #6
  7. levent

    Pete Becker Guest

    Frank Chang wrote:
    > 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';

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Aug 19, 2005
    #7
  8. levent

    Frank Chang Guest

    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.

    >>- 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).


    Could you please tell me why this is true? As Victor said, std::getline
    will handle a line of arbitrary length.
    Frank Chang, Aug 19, 2005
    #8
  9. levent

    levent Guest

    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
    levent, Aug 19, 2005
    #9
  10. levent

    Frank Chang Guest

    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.
    Frank Chang, Aug 19, 2005
    #10
  11. levent

    Default User Guest

    Frank Chang wrote:

    > 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.
    >
    > > > - 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).



    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
    Default User, Aug 19, 2005
    #11
  12. levent

    Frank Chang Guest

    This is a test.
    Default User wrote:
    > Frank Chang wrote:
    >
    > > 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.
    > >
    > > > > - 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).

    >
    >
    > 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, Aug 19, 2005
    #12
  13. levent

    Default User Guest

    Frank Chang wrote:

    > This is a test.
    > Default User wrote:
    > > Frank Chang wrote:


    > > This has been a public service announcement.


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



    Brian
    Default User, Aug 19, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,291
    Matt Humphrey
    Oct 18, 2004
  2. magix

    determine last line of file entries

    magix, Sep 29, 2006, in forum: C Programming
    Replies:
    6
    Views:
    274
    Keith Thompson
    Oct 3, 2006
  3. Don Bruder
    Replies:
    3
    Views:
    951
    spikeysnack
    Aug 3, 2010
  4. Ak 756
    Replies:
    2
    Views:
    97
    Ak 756
    Aug 13, 2007
  5. Iñaki Baz Castillo

    Extract common entries in two arrays

    Iñaki Baz Castillo, Mar 14, 2009, in forum: Ruby
    Replies:
    4
    Views:
    145
    Harry Kakueki
    Mar 15, 2009
Loading...

Share This Page