-
Notifications
You must be signed in to change notification settings - Fork 771
Files, Part 1: Working with files
We've already seen open and fopen (todo link here) so let's look at some more advanced concepts.
#How do I tell how large a file is? For files less than the size of a long use fseek and ftell is a simple way to accomplish this:
Move to the end of the file and find out the current position.
fseek(f,0,SEEK_END);
long pos = ftell(f);
This tells us the current position in the file in bytes - i.e. the length of the file!
fseek
can also be used to set the absolute position.
fseek(f,0,SEEK_SET); // Move to the start of the file
fseek(f,posn,SEEK_SET); // Move to 'posn' in the file.
All future reads and writes in the parent or child processes will be honor this position. Note writing or reading from the file will change the current position.
See the man pages for fseek and ftell for more information.
Unlike position, closing a file stream is unique to each process. Other processes can continue to use their own file-handle.
Legal and Licensing information: Unless otherwise specified, submitted content to the wiki must be original work (including text, java code, and media) and you provide this material under a Creative Commons License. If you are not the copyright holder, please give proper attribution and credit to existing content and ensure that you have license to include the materials.