Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to C, how to set position in file? 1

Status
Not open for further replies.

thegman

Programmer
Oct 16, 2001
26
GB
I am reading in a big file (60MB) 5k at a time, which I find is a lot faster than byte-by-byte. The problem is, if the file does not divide exactly by 5k (which it clearly very rarely will), then I miss out on the last bytes of the file, i.e. when my program tries to pick up 5k but cannot get it. I tried to add a bit at the end which will pick up the last of the file byte-by-byte, which as it is less than 5k, is very fast, but the file pointer seems to have run past the end of the file, so I cannot get the rest of the data, so two questions, how can I rewind() the pointer, not to the start of the file, but to a given position (in bytes), and also how can I find out where the pointer is?

Thanks a lot.

Garry
 
fseek(filepointer,0L,SEEK_SET) will put you to beginning of file
longint=ftell(filepointer) will return current position
so
fseek(filepointer,longint,SEEK_SET) will put you where you were previously
DB :) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Exactly what I wanted, thanks a lot.

Garry
 
A few things to add:

rewind(fp);

is equivalent to

fseek(fp,0L,SEEK_SET);

theegg: How are you reading the file? The reason I ask is because the problem you're having could probably be solved by changing your loop termination condition.

In other words, you should only exit the loop when your read function indicates an error or end-of-file. Getting less than the amount you ask for (which will happen on the final read) shouldn't be regarded as an error.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top