osnat
asked on
easy q about files and reading from them
hello all!
i have a realy easy q:
I have an ascii file. I need to read the last line in the file (I don't know how long is that line, or any other line in the file for that matter).
after reading the line, I need to see if it's the same as my indicator (9999). the line must contain only that indicator. if not - I need to go to the prev line and do the check again, until I'll find a line that contains only 9999 or I've reached the begining of the file.
the reason for going backwords is because the file can contain several (can be a lot of) lines with my indicator, and I need to find the last line.
asjhghjg
jhasgfg jkhfyytrwui
kags kjhadj
9999
kjaf
kdf jgsg hjghjgf qkudh
9999
when I find the last line that contains the indicator - I need to set up a file pointer to point to the line after (from there I should start writing something new).
hope to get some help
i have a realy easy q:
I have an ascii file. I need to read the last line in the file (I don't know how long is that line, or any other line in the file for that matter).
after reading the line, I need to see if it's the same as my indicator (9999). the line must contain only that indicator. if not - I need to go to the prev line and do the check again, until I'll find a line that contains only 9999 or I've reached the begining of the file.
the reason for going backwords is because the file can contain several (can be a lot of) lines with my indicator, and I need to find the last line.
asjhghjg
jhasgfg jkhfyytrwui
kags kjhadj
9999
kjaf
kdf jgsg hjghjgf qkudh
9999
when I find the last line that contains the indicator - I need to set up a file pointer to point to the line after (from there I should start writing something new).
hope to get some help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nah... Better to read a large block and perform the search in memory;
fseek(file, sizeof(buffer), SEEK_END);
fread(buffer, 1, sizeof(buffer);
for (i = 0; i < sizeof(buffer); i++)
if (buffer[sizeof(buffer) - i] == '\n') {
fseek(file, i, SEEK_END);
break;
}
/* if no \n is found, move back one block and try again... */
fseek(file, sizeof(buffer), SEEK_END);
fread(buffer, 1, sizeof(buffer);
for (i = 0; i < sizeof(buffer); i++)
if (buffer[sizeof(buffer) - i] == '\n') {
fseek(file, i, SEEK_END);
break;
}
/* if no \n is found, move back one block and try again... */
Oops... I guess some minor offset adjustments are wrong. Use for (i = 1... (or even 2 to skip empty last line) for example. And I'm not entirely sure about the last fseek. It probably should take i - 2...
ASKER
reading "character by character " is not so good.
I used what u said about starting to search from a location that is a portion of the file's size, but I did it with fgets.
I used what u said about starting to search from a location that is a portion of the file's size, but I did it with fgets.
However, what I would normally do (unless this is a very very large file) is read the file from beginning to end. For each line determine whether it is "9999", and, if it is, save a pointer to it. If you find another one, update the pointer. At the end of the process the pointer will point to the last instance.