getline problem

Discussion in 'C++' started by Kay, Aug 30, 2004.

  1. Kay

    Kay Guest

    I have tried to use following code to read data form a txt file. However
    , I have found that it would obtain last sentence one more time. How can
    I solve this problem ?

    void load(istream& is) {

    string temp_name, temp_cuisine, temp_mode;

    while( is != NULL ){

    getline(is, temp_name, ';');
    getline(is, temp_cuisine, ';');
    getline(is, temp_mode, '\n');

    cout << temp_name;
    cout << temp_cuisine;
    cout << temp_mode << endl;

    }

    THE code display :

    C:\DJGPP\myFiles\sc>a
    John Jordan 6 8
    Peter 5 9
    Kay 2 6
    Loen 8 0
    Loen 8 0 <-- display one more

    C:\DJGPP\myFiles\sc>
    Kay, Aug 30, 2004
    #1
    1. Advertising

  2. Kay

    Sharad Kala Guest

    "Kay" <> wrote in message

    Always check the FAQ before posting -
    http://www.parashift.com/c -faq-lite/input-output.html#faq-15.5

    > I have tried to use following code to read data form a txt file. However
    > , I have found that it would obtain last sentence one more time. How can
    > I solve this problem ?
    >
    > void load(istream& is) {
    >
    > string temp_name, temp_cuisine, temp_mode;
    >
    > while( is != NULL ){

    while ( is >> temp_name >> temp_cuisine >> temp_mode) {

    >
    > getline(is, temp_name, ';');
    > getline(is, temp_cuisine, ';');
    > getline(is, temp_mode, '\n');
    >


    -Sharad
    Sharad Kala, Aug 30, 2004
    #2
    1. Advertising

  3. Kay

    Kay Guest

    I have tried to sth like this

    char a;
    while( is >> a ) {....}
    each sentences are omitted the first character. eg

    C:\DJGPP\myFiles\sc>a
    ohn Jordan 6 8 <-- John
    eter 5 9 <-- Peter
    ay 2 6 <-- Kay
    oen 8 0 <-- Leon

    Kay wrote:
    > I have tried to use following code to read data form a txt file. However
    > , I have found that it would obtain last sentence one more time. How can
    > I solve this problem ?
    >
    > void load(istream& is) {
    >
    > string temp_name, temp_cuisine, temp_mode;
    >
    > while( is != NULL ){
    >
    > getline(is, temp_name, ';');
    > getline(is, temp_cuisine, ';');
    > getline(is, temp_mode, '\n');
    >
    > cout << temp_name;
    > cout << temp_cuisine;
    > cout << temp_mode << endl;
    >
    > }
    >
    > THE code display :
    >
    > C:\DJGPP\myFiles\sc>a
    > John Jordan 6 8
    > Peter 5 9
    > Kay 2 6
    > Loen 8 0
    > Loen 8 0 <-- display one more
    >
    > C:\DJGPP\myFiles\sc>
    >
    Kay, Aug 30, 2004
    #3
  4. Kay

    Jon Bell Guest

    In article <>,
    Kay <> wrote:
    >
    >void load(istream& is) {
    >
    > string temp_name, temp_cuisine, temp_mode;
    >
    > while( is != NULL ){
    >
    > getline(is, temp_name, ';');
    > getline(is, temp_cuisine, ';');
    > getline(is, temp_mode, '\n');
    >
    > cout << temp_name;
    > cout << temp_cuisine;
    > cout << temp_mode << endl;
    >
    > }


    In both C and C++, input does not trigger an end-of-file condition until
    *after* you have *tried* to read past the physical end of file and
    *failed*. Simply reading the last item from the file doesn't trigger an
    end-of-file condition. Therefore you must always check for end-of-file
    *after* you have (tried to) read something. In your example, the standard
    C++ idiom for this looks like this:

    while( getline(is, temp_name, ';')
    && getline(is, temp_cuisine, ';')
    && getline(is, temp_mode, '\n') ){

    cout << temp_name;
    cout << temp_cuisine;
    cout << temp_mode << endl;

    }

    This works because all C++ input expressions evaluate as true/false in a
    boolean context, depending on whether the input operation succeeded or
    not. This also works with the stream extraction operator, for example:

    while (is >> x)
    {
    // do something with x
    }

    If you're in a situation where input can fail without reaching the end of
    file (e.g. when reading numeric data), follow the loop with a test for
    eof():

    if (is.eof())
    {
    cout << "End of file reached." << endl;
    }
    else
    {
    cout << "Input error." << endl;
    }

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Aug 30, 2004
    #4
    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. John
    Replies:
    10
    Views:
    10,020
    Buster Copley
    Jul 14, 2003
  2. Poof
    Replies:
    1
    Views:
    369
  3. Joe
    Replies:
    4
    Views:
    7,736
    Chris Theis
    Jan 22, 2004
  4. solartimba

    getline problem

    solartimba, Apr 6, 2004, in forum: C++
    Replies:
    1
    Views:
    417
    Leor Zolman
    Apr 6, 2004
  5. Alex

    getline problem

    Alex, Jun 5, 2004, in forum: C++
    Replies:
    6
    Views:
    496
    Victor Bazarov
    Jun 6, 2004
Loading...

Share This Page