File positioning in C refers to the methods used to move the file position indicator, which determines where the next read or write operation will occur in a file. The C Standard Library provides several functions to manipulate the file position indicator, allowing you to read from or write to specific locations in a file.

Key Functions for File Positioning

  1. fseek()
  2. ftell()
  3. rewind()

1. fseek()

The fseek() function is used to move the file position indicator to a specific location in the file.

Syntax:

int fseek(FILE *stream, long offset, int whence);
  • stream: A pointer to the file you want to position.
  • offset: The number of bytes to move the position indicator. This value can be positive or negative.
  • whence: The reference point from which to apply the offset. It can take the following values:
    • SEEK_SET: The beginning of the file. The offset is added to the start.
    • SEEK_CUR: The current position in the file. The offset is added to the current position.
    • SEEK_END: The end of the file. The offset is added to the end of the file (often negative to move back).

Example:

#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } // Move the file position to the 10th byte from the beginning fseek(file, 10, SEEK_SET); // Now read from that position char buffer[20]; fread(buffer, sizeof(char), 10, file); buffer[10] = '\0'; // Null-terminate the string printf("Data from position 10: %s\n", buffer); fclose(file); return 0; }

2. ftell()

The ftell() function returns the current position of the file position indicator in bytes from the beginning of the file.

Syntax:

long ftell(FILE *stream);
  • stream: A pointer to the file.

Example:

#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } // Move to a specific position fseek(file, 10, SEEK_SET); // Get the current position long position = ftell(file); printf("Current position: %ld\n", position); // Should print 10 fclose(file); return 0; }

3. rewind()

The rewind() function sets the file position indicator to the beginning of the file. It also clears the end-of-file indicator.

Syntax:

void rewind(FILE *stream);
  • stream: A pointer to the file.

Example:

#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } // Read the first 10 characters char buffer[11]; fread(buffer, sizeof(char), 10, file); buffer[10] = '\0'; // Null-terminate the string printf("First 10 characters: %s\n", buffer); // Rewind to the beginning of the file rewind(file); // Read again from the beginning fread(buffer, sizeof(char), 10, file); printf("Again the first 10 characters: %s\n", buffer); fclose(file); return 0; }

Important Considerations

  • File Position Indicator: The file position indicator is critical for reading and writing data at specific locations in a file. It is automatically updated with each read or write operation.

  • Binary vs. Text Files: File positioning is especially important when working with binary files, where specific byte locations are crucial. For text files, line endings and formatting may affect positioning.

  • Error Checking: Always check the return values of fseek() and ftell() for potential errors, especially when working with file pointers that may be invalid.

Summary

  • fseek(): Move the file position indicator to a specific location.
  • ftell(): Get the current position of the file position indicator.
  • rewind(): Reset the file position indicator to the beginning of the file.

Understanding file positioning in C allows for more advanced file manipulation techniques, enabling you to read and write data in a flexible and efficient manner.