Thread: reading files

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    10

    Question reading files

    I am reading from a data file sequentially, when i get to the last record in the file it reads it twice and outputs to my output file twice. I am using a while loop with a not end of file condtion, is there a place where i need to add an extra read or something??

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You may be reading past the end of the file within your loop. It is often easier to test the actual read operation for the end of the file rather than the state of the file. If the read fails then you can quit immeadiately.

    Otherwise you're reading the last record, eof isn't being set so the loop continues, and then you're reading again. Only when this next read is carried is eof then set, which means the remainder of you loop will be executed when you've already reached the end of the file.

    If this doesn't help, post some code.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    10
    here is the loop i tried to do an extra read an now it causes an infinite loop
    ifstream in("a://visi.txt", ios::in);
    ofstream out("c://opreg", ios:ut);
    if(!in)
    {
    cout<<"cannot open input file";
    return 1;
    }
    in >> report.accountnum;
    in >> report.lastname;
    while(!in.eof())
    {
    in >> report.accountnum;
    in >> report.lastname;
    out << defaultnum;
    out << report.accountnum;
    out << "M";
    if(strcmp(report.lastname, mchar) <0)
    {
    out<<fint<<endl;
    }
    else
    {
    out<<tint << endl;
    }
    in>> report.accountnum;

    }

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You don't want an extra read, you just need to test the outcome of an existing read. If you're sure that all records are correctly entered into the file you could do something like -

    while(in >> report.accountnum)
    {
    in >> report.lastname;
    //etc
    }

    instead of

    while(!in.eof())
    //etc

    for the reasons stated in my original post.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    10
    ok changed my loop to this:
    while(in>> report.accountnum)
    {
    in >> report.accountnum;
    in >> report.lastname;
    out << defaultnum;
    out << report.accountnum;
    out << "M";
    if(strcmp(report.lastname, mchar) <0)
    {
    out<<fint<<endl;
    }
    else
    {
    out<<tint << endl;
    }


    }
    but now it only does one read, it only grabs the first record

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You're reading something into report.accountnum twice each loop. Loose the second one, otherwise it'll make your file reading get out of sync resulting in a bad read (where something like a char is extracted when a int is expected) which will cause the stream to fail and force your loop to exit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM