end of file

This is a discussion on end of file within the C++ Programming forums, part of the General Programming Boards category; Hi I'm having trouble with breaking from a loop when I reach the end of a line. Basically the program ...

  1. #1
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23

    end of file

    Hi

    I'm having trouble with breaking from a loop when I reach the end of a line. Basically the program moves through each line, loads it into memory and checks a variable to see if it matches one input by a user. That works fine, if the variable is there it catches it but if it is not there it just hangs. I have printed out the result of in.eof() to the screen but all I seem to get is 1. I am using this code to check if it is the end of a file:

    Code:
    while(!in.eof())
    {
    
    //Read in Line and check variable
    
    }
    Any help that you could give me would be greatly appreciated.

    Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Don't worry, the rules are the same for C++ as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One big thing to know about eof() is that its only true after it attempts to do a read. As such its a poor loop conditional.

    Run this program:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      using namespace std;
      ifstream ifs("test.txt");
      string line;
      unsigned i=0;
      while ( !ifs.eof() )
      {
        i++;
        getline(ifs,line);
        cout<<"Up to "<<i<<" lines now.  eof() = "<<boolalpha<<ifs.eof()<<endl;
      }
    }
    with the following input
    Code:
    Line one
    line two
    line three
    line four
    make sure there is no newline at the end of "line four" and see what output you get. Then add in a newline and see what you get.

    Now without seeing how you are manipulating the input stream and without any type of test input it's hard to say what is causing your problem.

  4. #4
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Cheers! Got that working now

    I know I'm being a pain but is it possible to delete a specific line from a file or append a section of that line? My program basically reads in information from a file which may then be edited by the user. When this is done the new information has to be stored in the file again. Would it be easier to simply delete a line once it is read in and then write that line out again at the bottom of the file or would appending the existing line be the easiest?

    Any help would be appreciated.

    Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Text files have to be rewritten each time you make a change, unless you're simply appending to the end of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Oh now that is a pain in the rear. So to remove a single record from the file I'll have to copy all the records into the program one at a time, writing them into a new file before copying the next one. Then do the same writing them back into the existing file over any data already in it (so as to keep the same file name).

    Please tell me that's not what has to be done. Don't make a grown man cry

    Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well there are plenty of data structures in the C++ STL which make this a hell of a lot easier in C++ than it would be in C.
    Like for example a vector of strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21