Problems with ifstream and getline

Discussion in 'C++' started by Jim Phelps, Jan 21, 2004.

  1. Jim Phelps

    Jim Phelps Guest

    Hello all,
    I am in a bit of a pickle using the getline function with an ifstream.
    It does not seem to work as advertised. Here is my scenario. In a
    nutshell, my code needs to pick up a fixed record length flat file
    that is generated by an old IBM mainframe. These data fields need to
    be read in by my program EXACTLY as they are represented in the file.
    Here is an example text file that I used in my test.

    aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccc
    ddddddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffff
    gggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiii

    For the purposes of this test, I have made each "field" 26 characters
    in length, and three fields exist per line (record). I produced the
    following code in which to pull the first three fields from the first
    record in the file.

    #include <stdlib.h>
    #include <fstream.h>

    int main (int argc, char* args[])
    {
    ifstream fReader;

    char field1[27];
    char field2[27];
    char field3[27];

    fReader.open("data.txt", ios::in );

    if (!fReader)
    {
    cout << "Unable to open file!!!" << endl;
    return (1);
    }

    fReader.getline(field1, 27);
    fReader.getline(field2, 27);
    fReader.getline(field3, 27);

    cout << "Field 1: " << field1 << endl;
    cout << "Field 2: " << field2 << endl;
    cout << "Field 3: " << field3 << endl;

    if (fReader.eof())
    {
    cout << "End of file reached." << endl;
    }

    if (!fReader)
    {
    cout << "File error" << endl;
    return (1);
    }

    fReader.close();

    return (0);
    }

    I compiled this code using the CC compiler in Sun Workshop 6.2. No
    special compiler flags were included in the compile. The output of
    the program is the following.

    Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
    Field 2:
    Field 3:
    File error

    As you can see, it seems that after the first getline, the ifstream
    loses it's way as the next two getlines retreive no information at
    all. Doing the check of the ifstream at the end of the program
    indicates that there is a problem.

    What exactly am I doing wrong? This problem has driven me up the wall
    for the past two days, and I need a fresh set of eyes to shed some
    light on the problem.

    Thanks in advance,
    jp
     
    Jim Phelps, Jan 21, 2004
    #1
    1. Advertising

  2. Jim Phelps wrote:
    >
    > I compiled this code using the CC compiler in Sun Workshop 6.2. No
    > special compiler flags were included in the compile. The output of
    > the program is the following.
    >
    > Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
    > Field 2:
    > Field 3:
    > File error


    Cut&Paste to VC++ 6.0
    compiled, linked, run.

    The Output is:

    Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
    Field 2: bbbbbbbbbbbbbbbbbbbbbbbbb
    Field 3: ccccccccccccccccccccccccc

    Although this proves not much I take it as a strong indication
    that something is wrong with your compiler.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 21, 2004
    #2
    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,060
    Buster Copley
    Jul 14, 2003
  2. Joe
    Replies:
    4
    Views:
    7,756
    Chris Theis
    Jan 22, 2004
  3. Christopher Benson-Manica

    ifstream/getline

    Christopher Benson-Manica, Apr 1, 2004, in forum: C++
    Replies:
    15
    Views:
    28,543
    Leor Zolman
    Apr 1, 2004
  4. tinks
    Replies:
    1
    Views:
    626
  5. Assertor
    Replies:
    2
    Views:
    2,580
    Assertor
    Feb 26, 2006
Loading...

Share This Page