devxlogo

Repositioning a File Pointer

Repositioning a File Pointer

The fseek() function provides random file access. It’s declared in as follows:

 int fseek(FILE *fstream, int offset, int whence);

The function sets the file pointer associated with fstream to a new position that is offsetbytes from the location specified by whence. The argument whence can have one of these values:

 SEEK_SET // file's beginningSEEK_CUR // current file positionSEEK_END // end of file

In the following example, fseek() is used to calculate a file’s size in a portable manner:

 long file_size (FILE *f);{ long cur_pos, length; cur_pos = ftell(f) fseek(f, 0, SEEK_END); // set pointer to end of file length=ftell(f); // offset in bytes from file's beginning fseek(f, cur_pos, SEEK_SET); // restore original position return length;}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist