Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: eof bit not set

  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    eof bit not set

    why such two easy lines don work
    char c;
    while(!file.eof ())
    { file.read(c,1); cout<<c; }
    the loop is infinite i don know why the eof bit is not set or what

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I don't see what type of object file is, but if it is what I think it is, file.read is a seek operation. You need to advance the pointer.
    http://www.cplusplus.com/doc/tutorial/files.html

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don understand what u mean by advancing my pointer!!!

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Did you read the linked article that explains what that means?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that problem is done now
    but i got another one,when file.tellg() return -1?
    Code:
    while(file.tellg()<=lastEnd)
    {
    	file.getline (temp,20,'=');
    	if (strncmp(temp,varName.c_str(),varName.length())==0  )
    	{
    		file.getline (X,20);
    		return true;
    	}
    	file.ignore(30,'\n');
    }
    when i debug this code and make it print lastEnd and file.tellg() values through the iteration of the loop every thing is fine until file.tellg() return -1 in the middle of the file so the loop doesn't end??!!!!!

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Why are you doing all these seek and pointer operations?
    Are you moving back and forth in the stream or trying to skip to a certain position?

    Also you seem to be combining seek operation with whole getline. I don't think that would work very well.

    If you just need to read input forward-only and do something with it, you don't need all this. You can just read in a line at a time as a string and manipulate that string.

    What are you trying to do exactly? Get pairs of things from a file using = or look for a certain string in the file, or ...?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #7
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm trying to read a file on that form
    someThing = someValue
    and i don know that something in advance so i read it and compare it if that is it i take the value after '='.
    i know i can do it with different way may be reading the hole line then getting parts of it ,but i wanna know what's wrong with this approach??

  • #8
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Nothing "wrong" per se, but it's always best to keep things as simple as possible.
    If you know, for example, that you have a file in a particular format, such as name/value pairs (name=value then a newline), you can just read it in line per line, do a string split on the equals sign, and compare the array(0) element.

    If this in an ini file or a config file, there are probably routines to do what you're looking to do already made. It's rarely necessary to reinvent the wheel, unless there are no wheels out there that meet you needs, if that makes sense.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •