How do I stop reading input at the end of a line?

Discussion in 'C++' started by W. Cerven, Jun 8, 2004.

  1. W. Cerven

    W. Cerven Guest

    I have a list of n inputs of varying type I wish to read from a file:

    For example:
    string string int int double double int int int double ...

    My input file gives its values on a single line, separated by spaces
    or tabs.
    However, only the first k<=n of these values are given. Without
    knowing beforehand what k is, how do I read these k values to my
    variables and stop reading when I reach the end of the line?

    Thanks!
    W. Cerven, Jun 8, 2004
    #1
    1. Advertising

  2. W. Cerven wrote:
    > I have a list of n inputs of varying type I wish to read from a file:
    >
    > For example:
    > string string int int double double int int int double ...
    >
    > My input file gives its values on a single line, separated by spaces
    > or tabs.
    > However, only the first k<=n of these values are given. Without
    > knowing beforehand what k is, how do I read these k values to my
    > variables and stop reading when I reach the end of the line?


    Read the entire line (using 'getline') into std::string object and then
    parse it using std::istringstream.

    V
    Victor Bazarov, Jun 8, 2004
    #2
    1. Advertising

  3. W. Cerven wrote:

    > I have a list of n inputs of varying type I wish to read from a file:
    >
    > For example:
    > string string int int double double int int int double ...
    >
    > My input file gives its values on a single line,
    > separated by spaces or tabs.
    > However, only the first k <= n of these values are given.
    > Without knowing beforehand what k is,
    > how do I read these k values to my variables and stop reading
    > when I reach the end of the line?


    Why should you stop reading when you reach the end of the line?
    The end-0f-line character [sequence] is just another whitespace
    as far as C++ is concerned. It will find the k+1 value
    at the beginning of the next line.
    E. Robert Tisdale, Jun 8, 2004
    #3
  4. W. Cerven

    Jorge Rivera Guest

    W. Cerven wrote:
    > I have a list of n inputs of varying type I wish to read from a file:
    >
    > For example:
    > string string int int double double int int int double ...
    >
    > My input file gives its values on a single line, separated by spaces
    > or tabs.
    > However, only the first k<=n of these values are given. Without
    > knowing beforehand what k is, how do I read these k values to my
    > variables and stop reading when I reach the end of the line?
    >
    > Thanks!


    This will read every value into a string. It is up to you to convert it
    to the right type.

    If you know in advance what the type of your data is (fixed string
    string int int....), you can use the same technique, substituting s1 for
    the correct variable in which you want to store the values.

    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    std::ifstream file( <full-path-her> );

    std::string line;
    std::istringstream parser;

    if(file)
    {
    std::string value;
    while( std::getline(file, line) )
    {
    parser.clear();
    parser.str(line);

    while(parser)
    {
    parser>>value;
    std::cout<<"NextValue: "<<value<<std::endl;
    }
    }
    }
    return 0;
    }
    Jorge Rivera, Jun 8, 2004
    #4
  5. W. Cerven

    David Harmon Guest

    On Tue, 08 Jun 2004 14:20:12 -0700 in comp.lang.c++, "E. Robert Tisdale"
    <> wrote,
    >
    >Why should you stop reading when you reach the end of the line?
    >The end-0f-line character [sequence] is just another whitespace
    >as far as C++ is concerned.


    But apparently not as far as his desired input format is concerned.
    Perhaps each line contains one set of data to be handles separately.

    Perhaps some relative of
    while (cin.peek() == ' ')
    David Harmon, Jun 8, 2004
    #5
  6. W. Cerven

    Default User Guest

    "E. Robert Tisdale" wrote:

    > Why should you stop reading when you reach the end of the line?
    > The end-0f-line character [sequence] is just another whitespace
    > as far as C++ is concerned. It will find the k+1 value
    > at the beginning of the next line.


    And what if the end of a line denotes the completion of a record or
    other processing iteration?



    Brian Rodenborn
    Default User, Jun 9, 2004
    #6
  7. Troll Alert: How do I stop reading input at the end of a line?

    Something that calls itself Default User wrote:

    > E. Robert Tisdale wrote:
    >
    >>Why should you stop reading when you reach the end of the line?
    >>The end-0f-line character [sequence] is just another whitespace
    >>as far as C++ is concerned. It will find the k+1 value
    >>at the beginning of the next line.

    >
    > And what if the end of a line denotes
    > the completion of a record or other processing iteration?


    W. Cerven does may have meant but does not actually say that
    "the end of a line denotes he completion of a record
    or other processing iteration".

    I have added you to my killfile twice now
    but, unhappily, your trolls are still getting through.
    Can anybody tell me how to get my Mozilla 1.6 newsreader
    to killfile Default User once and for all?
    E. Robert Tisdale, Jun 9, 2004
    #7
  8. W. Cerven

    W. Cerven Guest

    Thanks for the help!

    In response to some of the earlier posts, yes, I am reading from a
    file in which each line is a separate data record. However, the
    latter inputs are optional and thus are not always provided. Thus, I
    want to collect all of the data provided, but don't know exactly how
    much is provided a priori.
    W. Cerven, Jun 9, 2004
    #8
  9. Re: Troll Alert: How do I stop reading input at the end of a line?

    E. Robert Tisdale wrote:

    > Something that calls itself Default User wrote:
    >
    >> E. Robert Tisdale wrote:
    >>
    >>> Why should you stop reading when you reach the end of the line?
    >>> The end-0f-line character [sequence] is just another whitespace
    >>> as far as C++ is concerned. It will find the k+1 value
    >>> at the beginning of the next line.

    >>
    >>
    >> And what if the end of a line denotes
    >> the completion of a record or other processing iteration?

    >
    >
    > W. Cerven does may have meant but does not actually say that
    > "the end of a line denotes he completion of a record
    > or other processing iteration".
    >
    > I have added you to my killfile twice now
    > but, unhappily, your trolls are still getting through.
    > Can anybody tell me how to get my Mozilla 1.6 newsreader
    > to killfile Default User once and for all?


    Your definition of Troll needs redefining.
    "Default User", Brian, was just pointing out an exception
    or another case to test your assumption statement. He
    was not trolling.

    Also, I think you "This is obviously a troll, please ignore"
    needs some refining, as some non-troll posts have been
    erronously labelled.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jun 9, 2004
    #9
  10. W. Cerven wrote:

    > Thanks for the help!
    >
    > In response to some of the earlier posts, yes, I am reading from a
    > file in which each line is a separate data record. However, the
    > latter inputs are optional and thus are not always provided. Thus, I
    > want to collect all of the data provided, but don't know exactly how
    > much is provided a priori.


    Ahhh, optional fields and variable records.
    As others have stated, I suggest that you read the line into
    a string and then parse the string. Pass the string to functions
    which extract their arguments. This is how I am currently parsing
    my data {except I use an object for each field}.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jun 9, 2004
    #10
  11. W. Cerven

    Default User Guest

    Re: Troll Alert: How do I stop reading input at the end of a line?

    "E. Robert Tisdale" wrote:
    >
    > Something that calls itself Default User wrote:


    > > And what if the end of a line denotes
    > > the completion of a record or other processing iteration?

    >
    > W. Cerven does may have meant but does not actually say that
    > "the end of a line denotes he completion of a record
    > or other processing iteration".


    He didn't say he didn't either. You made the assumption that end of line
    meant nothing, without asking.

    > I have added you to my killfile twice now
    > but, unhappily, your trolls are still getting through.
    > Can anybody tell me how to get my Mozilla 1.6 newsreader
    > to killfile Default User once and for all?


    Trollsdale doesn't even remember that he KFed me in a different
    newsgroup. Doesn't matter, I'll keep pointing out his bullshit
    regardless. I don't care whether *he* sees my posts, it's the new and
    untrained that can be mislead by his vicious misleading "information".




    Brian Rodenborn
    Default User, Jun 9, 2004
    #11
  12. W. Cerven

    Default User Guest

    Re: Troll Alert: How do I stop reading input at the end of a line?

    Thomas Matthews wrote:

    > Your definition of Troll needs redefining.
    > "Default User", Brian, was just pointing out an exception
    > or another case to test your assumption statement. He
    > was not trolling.


    It's usually considered an honor for Trollsdale to label you a Troll.
    You guys should check out his latest shenanigans on comp.lang.c.

    > Also, I think you "This is obviously a troll, please ignore"
    > needs some refining, as some non-troll posts have been
    > erronously labelled.


    I'd say 90% of what he labels as a troll post is in fact one that merely
    disagrees with him.



    Brian Rodenborn
    Default User, Jun 9, 2004
    #12
    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. Will
    Replies:
    1
    Views:
    15,189
    Thomas Weidenfeller
    Nov 2, 2004
  2. dbuser
    Replies:
    8
    Views:
    502
    Thomas J. Gritzan
    Oct 10, 2005
  3. omariqbalnaru
    Replies:
    3
    Views:
    443
    David Harmon
    Nov 23, 2006
  4. Andreas Leitgeb
    Replies:
    0
    Views:
    436
    Andreas Leitgeb
    May 15, 2009
  5. Mark Space
    Replies:
    0
    Views:
    471
    Mark Space
    May 15, 2009
Loading...

Share This Page