Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Is there any fastest way to move "In a file" other than using the calls fseek or lseek.
Posted

Actually fseek and lseek do nothing more than setting an integer - the file pointer - somewhere in the open handle/descriptor. You can not make this faster I think. If you open a file an seek to its 1000000th byte fseek wont read up 1000000th bytes from the file, it just sets the integer mentioned earlier. Its even legal to seek to an offset that is bigger than the size of the file itself. If the file pointer is set to a value bigger than the file size then fread() calls will return without error indicating that only zero bytes could be read. fwrite() in the same situation will grow up the file size to the specified offset and then writes out your data. "Growing up" the file to reach the actual file pointer might take some time on some systems because of security reasons - it is a good habit (of some OSes) to overwrite the previous contents of the sectors that are allocated to fill in the hole between the end of the file and your file pointer because they might contain sensitive data from some files deleted by someone else (for example a text file with his passwords). On windows I know that with administrative privileges there are API calls to turn off this overwrite procedure when preallocating file space.

EDIT: anyway, in a text file you don't want to fseek. I've never seen such a program. With text file you either open for read with the file pointer at zero (and you read the file line-by-line), or open for write with file pointer at zero and filesize set to zero (original file contents are deleted), or you open for write by appending to the original contents and the filepointer set to the end of file immediately after open. Theoritically you can read a text file character-by-character but I don't know what happens if you mix functions so that one of them works with just characters (fgetc) and the other works with lines (fgets) but in practice I never in need of this.
 
Share this answer
 
v4
Not that I am aware of. Why do you think there could be, since the seek() functions complete in (probably) sub-nanosecond time?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900