In C, file processing allows you to store and manipulate data outside of the program's memory. Files are an essential component of most applications, as they provide a way to save data between program executions. File handling in C is done using streams, and the operations you perform on files depend on the type of file—sequential or random-access.
In C, files are handled as streams. A stream is a flow of data between your program and a file. There are two types of streams:
A file in C is represented by a FILE pointer, which is used by various functions from the stdio.h library to open, read, write, and close files.
fopen() function.fread(), fwrite(), fscanf(), and fprintf().fclose() function.When opening a file with fopen(), you must specify the mode in which the file will be used. The most common modes are:
"r": Open for reading (file must exist)."w": Open for writing (creates a new file or truncates an existing file)."a": Open for appending (create a new file or add to the end of an existing file)."rb", "wb", "ab": Same as the above, but for binary files."r+": Open for both reading and writing."w+": Open for both reading and writing (creates a new file or truncates an existing one)."a+": Open for reading and appending (creates a new file or appends to the end).In sequential files, data is written or read in a linear fashion, one record after another. Each read or write operation starts from the beginning and goes forward in the file.
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w"); // Open file in write mode
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
fprintf(file, "Hello, world!\n"); // Write a string to the file
fprintf(file, "This is a sequential file example.\n");
fclose(file); // Close the file
printf("File written successfully.\n");
return 0;
}
fopen("example.txt", "w"): Opens a file for writing. If it doesn’t exist, it will be created.fprintf(): Writes formatted data (like printf()) to the file.fclose(): Closes the file after the operation is complete.#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
char line[100];
while (fgets(line, sizeof(line), file)) { // Read one line at a time
printf("%s", line); // Print each line from the file
}
fclose(file); // Close the file
return 0;
}
fopen("example.txt", "r"): Opens the file for reading.fgets(): Reads a line from the file into the line buffer.while loop continues reading lines from the file until it reaches the end.Hello, world!
This is a sequential file example.
A random-access file allows you to move the file pointer to any position in the file and read or write data from that specific location. This is especially useful when dealing with large data sets or when you need to access individual records at specific positions in the file without reading the entire file sequentially.
To work with random-access files, you typically use fseek(), ftell(), and fread()/fwrite().
fseek(FILE *stream, long offset, int whence): Moves the file pointer to a specified location.
offset: The number of bytes to move the pointer.whence: The reference point (e.g., SEEK_SET for the beginning, SEEK_CUR for current position, SEEK_END for the end).ftell(FILE *stream): Returns the current position of the file pointer.fread() and fwrite(): Read and write blocks of data from or to a file at the current pointer position.#include <stdio.h>
struct Student {
int id;
char name[50];
};
int main() {
FILE *file = fopen("students.dat", "wb+"); // Open in binary read-write mode
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
struct Student s1 = {1, "Alice"};
struct Student s2 = {2, "Bob"};
// Writing to the file
fwrite(&s1, sizeof(struct Student), 1, file); // Write s1 to the file
fwrite(&s2, sizeof(struct Student), 1, file); // Write s2 to the file
// Move the file pointer to the beginning
fseek(file, 0, SEEK_SET);
// Reading from the file
struct Student s3, s4;
fread(&s3, sizeof(struct Student), 1, file); // Read the first student
fread(&s4, sizeof(struct Student), 1, file); // Read the second student
printf("Student 1: %d, %s\n", s3.id, s3.name);
printf("Student 2: %d, %s\n", s4.id, s4.name);
fclose(file); // Close the file
return 0;
}
Student with an integer id and a name.fwrite() is used to write the Student structure to the file in binary mode.fseek() moves the file pointer back to the beginning of the file so we can read the data we just wrote.fread() is used to read the structures from the file at the current file pointer position.Student 1: 1, Alice
Student 2: 2, Bob
Binary files store data in the same format as it is in memory, unlike text files, which store data as human-readable characters. You use binary mode ("wb", "rb", etc.) to read and write binary files.
#include <stdio.h>
int main() {
FILE *file = fopen("data.bin", "wb"); // Open in binary write mode
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
int numbers[] = {1, 2, 3, 4, 5};
fwrite(numbers, sizeof(int), 5, file); // Write array to binary file
fclose(file);
return 0;
}
#include <stdio.h>
int main() {
FILE *file = fopen("data.bin", "rb"); // Open in binary read mode
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
int numbers[5];
fread(numbers, sizeof(int), 5, file); // Read array from binary file
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
fclose(file);
return 0;
}
1 2 3 4 5
File processing in C allows you to store and manipulate large datasets efficiently. The operations for file handling can be divided into two categories based on the type of file:
By using functions like fopen(), fread(), fwrite(), fseek(), and ftell(), you can perform a variety of file operations, ranging from simple text-based file reading to more complex binary file manipulation.
Open this section to load past papers