C Programming
About Lesson
1: Using fseek, ftell, and rewind Functions

The fseek, ftell, and rewind functions are used to manipulate the file position indicator and navigate within files.

  • fseek Function:

    • Moves the file position indicator to a specified location within the file.
     
    int fseek(FILE *stream, long offset, int origin);
  • ftell Function:

    • Returns the current position of the file position indicator.
     
    long ftell(FILE *stream);
  • rewind Function:

    • Moves the file position indicator to the beginning of the file (equivalent to fseek(filePtr, 0, SEEK_SET)).
     
    void rewind(FILE *stream);
 2: Binary File Operations

In addition to text file operations, C supports binary file operations for reading and writing raw data.

  • Opening a Binary File:

    • Use "rb" mode for reading binary files and "wb" mode for writing binary files.
     
    FILE *binaryFile = fopen("data.bin", "wb");
  • Reading and Writing Binary Data:

    • Use fread() and fwrite() functions for binary data operations.
     
    // Writing binary data to a file
    int data[] = {10, 20, 30, 40, 50};
    fwrite(data, sizeof(int), 5, binaryFile);

    // Reading binary data from a file
    int buffer[5];
    fread(buffer, sizeof(int), 5, binaryFile);

3: Practice and Examples
  • Practice Exercise:
    • Write a program that reads integers from the user, stores them in an array, and writes the array contents to a binary file.
    • Implement a function that reads data from a binary file and displays it on the console.
    • Develop a program that uses fseek and ftell to navigate within a binary file and extract specific data.
#include <stdio.h>
#include <stdlib.h>

// Function to write array of integers to a binary file
void writeIntegersToFile(const char *filename, int *array, int size) {
FILE *binaryFile = fopen(filename, "wb");
if (binaryFile == NULL) {
printf("Error opening file for writing!n");
exit(1);
}

fwrite(array, sizeof(int), size, binaryFile);

fclose(binaryFile);
}

// Function to read integers from a binary file and display them
void readIntegersFromFile(const char *filename, int size) {
FILE *binaryFile = fopen(filename, "rb");
if (binaryFile == NULL) {
printf("Error opening file for reading!n");
exit(1);
}

int *buffer = (int *)malloc(size * sizeof(int));
fread(buffer, sizeof(int), size, binaryFile);

printf("Contents of binary file:n");
for (int i = 0; i < size; i++) {
printf("%d ", buffer[i]);
}
printf("n");

free(buffer);
fclose(binaryFile);
}

int main() {
int numIntegers;
printf("Enter the number of integers: ");
scanf("%d", &numIntegers);

int *integers = (int *)malloc(numIntegers * sizeof(int));
if (integers == NULL) {
printf("Memory allocation failed!n");
exit(1);
}

printf("Enter %d integers:n", numIntegers);
for (int i = 0; i < numIntegers; i++) {
scanf("%d", &integers[i]);
}

writeIntegersToFile("data.bin", integers, numIntegers);
readIntegersFromFile("data.bin", numIntegers);

free(integers);
return 0;
}

Binary file operations allow efficient storage and retrieval of raw data in files. Practice using fwrite() and fread() functions to perform binary file I/O operations. Understand the use of fseek, ftell, and rewind functions for file positioning and navigation within binary files. Experiment with different scenarios to gain experience in working with binary files and handling binary data effectively in C programming. Understanding binary file operations is essential for tasks such as data serialization, file compression, and low-level data processing in C applications.