Welcome to this lesson on File Positioning in C programming! Understanding how to navigate and manipulate files is crucial for any programmer working with data storage. In this tutorial, we will learn about the various file positioning functions that allow you to move around within a file, read specific parts of it, and perform operations based on the file position.
The C Standard Library provides several functions to manage file positions:
rewind()
: This function repositions the file pointer for the stream pointed by stream
to the beginning of the file.// Open a file for reading in binary mode
file = fopen("example.bin", "rb");
// Rewind the file pointer to the start
rewind(file);
```
fseek()
: This function positions the file pointer associated with the stream pointed to by stream
for the file identified by the argument file
, to an offset determined by offset
from a reference point specified by whence
.// Open a file for reading in binary mode
file = fopen("example.bin", "rb");
// Move the file pointer to a specific offset
fseek(file, 1024, SEEK_SET);
``
The
SEEK_SETconstant specifies that the offset is from the beginning of the file, while
SEEK_CURand
SEEK_END` indicate the current position and end of the file respectively.
ftell()
: This function returns the current position in the file associated with the stream pointed to by stream
.// Open a file for reading in binary mode
file = fopen("example.bin", "rb");
// Read 1024 bytes from the file
char buffer[1024];
fread(buffer, sizeof(char), 1024, file);
// Get the current file position
long pos = ftell(file);
printf("Current file position: %ld\n", pos);
```
Let's say we have a text file containing student information with one line per record (name, age, grade), separated by commas. We want to read only the names and ages of students whose grades are above 85.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_NAME_LEN 100
#define MAX_RECORDS 1000
int main() {
FILE *file;
char name[MAX_NAME_LEN], record[MAX_RECORDS][3][MAX_NAME_LEN];
int age, grade, i = 0;
// Open the file for reading
file = fopen("students.txt", "r");
if (file == NULL) {
perror("Error opening students.txt");
return 1;
}
while (!feof(file)) {
// Read a record from the file
fscanf(file, "%[^,],%d,%d", record[i][0], &age, &grade);
// Skip records with grades below 85
if (grade < 85) continue;
// Move to the next field (skip comma)
fgetc(file);
// Read and store the name
fgets(record[i][1], MAX_NAME_LEN, file);
printf("Name: %s\nAge: %d\n", record[i][0], age);
i++;
}
fclose(file);
return 0;
}
In this example, we use fseek()
and ftell()
to move the file pointer to the beginning of the file after reading each record. This allows us to read through the entire file multiple times without having to rewind it.
ftell()
)What causes it:
#include <stdio.h>
FILE *file;
long pos = ftell(file); // file is NULL
Error message:
Segmentation fault (core dumped)
Solution:
// Check if the file pointer is valid before using it
if (file != NULL) {
long pos = ftell(file);
}
Why it happens: Accessing memory without a valid pointer leads to undefined behavior, including segmentation faults.
How to prevent it: Always check if a pointer is valid before dereferencing it.
whence
value)What causes it:
#include <stdio.h>
FILE *file;
fseek(file, 1024, 3); // SEEK_END + 1024
Error message:
Invalid argument to fseek()
Solution:
// Use valid constants for the `whence` argument (SEEK_SET, SEEK_CUR, or SEEK_END)
fseek(file, 1024, SEEK_SET); // Set file position to 1024 bytes from the beginning
Why it happens: The whence
argument must be one of the valid constants provided by the C Standard Library. Using an invalid value leads to undefined behavior.
How to prevent it: Always use the correct constants when specifying the reference point for file positioning operations.
rewind()
sparingly, as it can be less efficient than moving the file pointer directly to a specific location.fseek()
on binary files and text files with different line endings (e.g., Windows vs Unix).rewind()
, fseek()
, and ftell()
functions allow you to manage the position of the file pointer in C.fseek()
(SEEK_SET, SEEK_CUR, and SEEK_END).With a solid understanding of file positioning, you will be well-equipped to navigate through large files and manipulate data as needed. Happy coding!