Thread: Simple End of file problem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Simple End of file problem

    Hmm i'm having a simple end of file problem... here's my code


    ifstream file;
    file.open("test.txt");

    while ( !file.eof() )
    {
    file.getline(temp_value, 30);
    cout << temp_value;
    file.getline(temp_value, 15);
    cout << temp_value;
    }
    file.close();

    this really isn't my code but is just the basis of it.. anyways this code should keep getting lines from file and displaying them unless file is empty right? But when i run my program it just loops forever it never quits the loop... and i only have 4 lines in
    test.txt..... any ideaS?

  2. #2
    Unregistered
    Guest
    Shot's in the dark, but what the hey...it's late at night and I feel like making a fool of myself.

    why does temp_value have two different sizes? probably ok, but looks a little wierd.

    -----------------------------------------

    try commenting out one set of the two lines in the loop, and try compiling. Then comment out the other set and recompile. then try again with both sets. If both work ok alone but not when combined try this:

    //prime the stream with the first line
    file.getline(temp_value, 30);

    while ( !file.eof() )
    {
    cout << temp_value;
    file.getline(temp_value, 15);
    cout << temp_value;
    file.getline(temp_value, 30);
    }


    -------------------------------------------------

    are you srue there are newline chars in your file?

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I have a feeling your file data is bad... make sure you have a return (newline) on the last line otherwise you will jam up your buffer.getline() function because it is looking for a newline character to terminate.

    There are better ways to do this, but I do not suppose you can wait 3 weeks for my file I/O tutorial.

    what is temp_value? is it a character array?

    look into the many uses of .get() and .put()
    Blue

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Talking THnx

    TStringList *title_list;
    TStringList *type_list;
    TStringList *size_list;
    TStringList *location_list;

    title_list = new TStringList;
    type_list = new TStringList;
    size_list = new TStringList;
    location_list = new TStringList;

    char temp_value[40];

    index = 0;

    while (!file.eof() )
    {
    file.getline(temp_value, 35);
    title_list->insert(index, temp_value.c_str());
    file.getline(temp_value, 25);
    type_list->insert(index, temp_value.c_str());
    file.getline(temp_value,7);
    size_list->insert(index,temp_value.c_str());
    file.getline(temp_value, 20);
    location_list->insert(index, temp_value.c_str());
    index = index + 1;
    }

    this was actually more like my original code.... except all the declarations are buried in classes and functions...

    what do u mean by "return (newline) on the last line"

    currently an example of my file would look like

    sim city
    game
    54 MB
    cd 1
    nero
    burning ap
    13 MB
    cd 4

    since my file always has the number of lines divisible by 4 wouldn't the loop end?

    jam up your buffer.getline() sounds like what i'm doing...
    what do u mean by newline character to terminate? just a blank line will work, right?

    the different sizes for temp_value are just because i don't want any excess data from those lines by accident.. i could of kept temp_value constant but i think it works the same.. i hope..

    yeah i've tested it without the loop and it works fine so i know the getline part works, just not sure why the loop goes on and on.... i'm probably making a stupid error somewhere.. "damn you programming.. damn you to hell!!!"

    guess i should look into .get and .put probably very usefull
    Last edited by tegwin; 03-04-2002 at 08:29 AM.

  5. #5
    Unregistered
    Guest
    string classes like TString often have amember function that returns the c_style string upon which each instance of the TString is based. That function is often called c_str(). However, temp_value IS a c_style string. It is NOT an instance of a class. It cannot have member functions like c_str(). If this is how your code is actually written fix that and see if it works ok.

    when you wrote material to the file does it look something like this:


    fout << title << endl;
    fout << type << endl;

    or something like this:

    fout << size << '\n';
    fout << location << '\n';

    such that there is a new line char at the end of each line?

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Thumbs up hmm

    actually i don't have my code in front of me i was just trying to guess what i had..i probably don't have temp_value.c_str()

    yeah i wrote

    index = 0;

    while ( index < 2 )
    {
    fout << title << endl;
    fout << type << endl;
    fout << size << endl;
    fout << location << endl;
    index = index + 1;
    }

    that is correct right? maybe i should just spill soda on my keyboard or something that might fix my code.... a little short circuit never hurts...

  7. #7
    Unregistered
    Guest
    when you get the chance try posting your actual code. The examples you have posted look like they should work. Have you tried the priming read yet? I've had off by one errors that were a pain the you know where when I didn't do that.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    hmm

    hmm somehow i fixed it and i don't know what i did.. .. strange.. but oh well... now codes works almost flawlessly.... thnx for the help
    Last edited by tegwin; 03-04-2002 at 01:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM