...

View Full Version : eof bit not set



mad_girl
10-05-2007, 11:32 PM
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

nikkiH
10-05-2007, 11:46 PM
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

mad_girl
10-06-2007, 12:12 AM
i don understand what u mean by advancing my pointer!!!

nikkiH
10-08-2007, 04:09 PM
Did you read the linked article that explains what that means?

mad_girl
10-11-2007, 02:24 AM
that problem is done now
but i got another one,when file.tellg() return -1?


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??!!!!!

nikkiH
10-11-2007, 03:47 PM
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 ...?

mad_girl
10-11-2007, 08:51 PM
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??

nikkiH
10-13-2007, 01:03 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum