0% found this document useful (0 votes)
527 views

File Positioning Functions

File positioning functions in C like fseek, ftell, and rewind allow programs to navigate and read specific parts of a file by manipulating the file pointer position. Fseek sets the file pointer position based on an offset and reference point. Ftell returns the current file pointer position. Rewind sets the file pointer to the beginning of the file. These functions make it possible to read or write data at arbitrary locations within a file.

Uploaded by

er.shalinichawla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
527 views

File Positioning Functions

File positioning functions in C like fseek, ftell, and rewind allow programs to navigate and read specific parts of a file by manipulating the file pointer position. Fseek sets the file pointer position based on an offset and reference point. Ftell returns the current file pointer position. Rewind sets the file pointer to the beginning of the file. These functions make it possible to read or write data at arbitrary locations within a file.

Uploaded by

er.shalinichawla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

File Positioning Functions

Dr. Shalini Gambhir


• In C programming, file positioning functions are used to set or
query the position of the file pointer within a file. The file pointer is
an indicator that points to the current position within the file, and
these functions help you navigate and manipulate it. The primary
file positioning functions in C are:
• fseek:
– int fseek(FILE *stream, long int offset, int whence);
– This function sets the file position of the stream to the given offset.
The 'whence' parameter specifies the reference point for the offset
(beginning, current position, or end of the file).
• cCopy code
• FILE *file = fopen("example.txt", "r"); fseek(file, 50, SEEK_SET); //
Move the file pointer to 50 bytes from the beginning of the file
• ftell:
• long int ftell(FILE *stream);
• This function returns the current file position
indicator for the specified stream.
• FILE *file = fopen("example.txt", "r"); long
position = ftell(file); // Get the current position
of the file pointer
• rewind:
• void rewind(FILE *stream);
• This function sets the file position indicator for
the specified stream to the beginning of the
file.
• FILE *file = fopen("example.txt", "r");
rewind(file); // Set the file pointer to the
beginning of the file
• fgetpos:
• int fgetpos(FILE *stream, fpos_t *pos);
• This function obtains the current file position
and stores it in the object pointed to by 'pos'.
The file position can later be restored using
fsetpos.
• FILE *file = fopen("example.txt", "r"); fpos_t
position; fgetpos(file, &position); // Get the
current position of the file pointer
• fsetpos:
• int fsetpos(FILE *stream, const fpos_t *pos);
• This function sets the file position indicator for
the specified stream to the value stored in 'pos'.
• cCopy code
• FILE *file = fopen("example.txt", "r"); fpos_t
position; // ... (set position using fgetpos or other
means) fsetpos(file, &position); // Set the file
pointer to the specified position
• These functions are useful for navigating within a
file, reading or writing data at specific locations,
and managing the file pointer based on your
requirements.
• Simple example using the file positioning
functions in C.
In this example, we'll create a file, write some
content to it, and then use the file positioning
functions to read specific portions of the file.
• #include <stdio.h>

• int main() {
• FILE *file;

• // Open a file in write mode
• file = fopen("example.txt", "w");

• // Write content to the file
• fprintf(file, "This is line 1.\n");
• fprintf(file, "This is line 2.\n");
• fprintf(file, "This is line 3.\n");

• // Close the file


• fclose(file);

• // Open the file in read mode


• file = fopen("example.txt", "r");

• // Use fseek to move the file pointer to the beginning of the second line
• fseek(file, 18, SEEK_SET);

• // Read and print the content from the second line


• char buffer[50];
• fgets(buffer, sizeof(buffer), file);
• printf("Content from the second line: %s", buffer);

• // Use ftell to get the current position of the file pointer


• long position = ftell(file);
• printf("Current position of the file pointer: %ld\n", position);

• // Use rewind to move the file pointer to the beginning of the file
• rewind(file);

• // Read and print the content from the beginning of the file
• fgets(buffer, sizeof(buffer), file);
• printf("Content from the beginning: %s", buffer);

• // Close the file


• fclose(file);

• return 0;
• Explanation:
• We open a file in write mode ("w") and write three lines of text to it
using fprintf.
• After closing the file, we open it again in read mode ("r").
• We use fseek to move the file pointer to the beginning of the
second line (character 18).
• We read and print the content from the second line using fgets.
• We use ftell to get the current position of the file pointer and print
it.
• We use rewind to move the file pointer back to the beginning of the
file.
• We read and print the content from the beginning of the file using
fgets.
• This example demonstrates how to use file positioning functions to
navigate within a file and read specific portions of its content.

You might also like