Random Access File in C

Random access to files in C programming allows you to read and write data at specific positions within the file, rather than sequentially from the beginning. To perform random access, you typically use functions like fseek to move the file pointer to a desired location.

To random access files in C programming, we can use the following functions:

  1. fseek(): This function sets the file pointer to a specific position in the file.
  2. ftell(): This function returns the current position of the file pointer in the file.
  3. rewind(): This function rewinds the file pointer to the beginning of the file.

To read or write data at a specific position in the file, we can use the fseek() function to set the file pointer to that position and then use the fread() or fwrite() functions to read or write the data.

Random Access Reads

Moving to a Specific Position (Using fseek)

To read data from a specific position in a file, use fseek to set the file pointer to that location.

FILE* file = fopen("data.bin", "rb"); if (file == NULL) { perror("File opening failed"); return 1; } fseek(file, 2 * sizeof(int), SEEK_SET); // Move to the third integer in the file int data; fread(&data, sizeof(int), 1, file); // Read an integer from the current position fclose(file);

Random Access with ftell and fseek

Use ftell to get the current position in the file, and then use fseek to move to a specific location, followed by reading data.

FILE* file = fopen("data.bin", "rb"); if (file == NULL) { perror("File opening failed"); return 1; } long position = ftell(file); // Get the current position fseek(file, position + sizeof(int), SEEK_SET); // Move one position forward int data; fread(&data, sizeof(int), 1, file); // Read an integer from the new position fclose(file);

Random Access Writes

Moving to a Specific Position for Writing (Using fseek)

To write data at a specific position in a file, use fseek to move the file pointer to that location.

FILE* file = fopen("data.bin", "rb+"); if (file == NULL) { perror("File opening failed"); return 1; } fseek(file, 2 * sizeof(int), SEEK_SET); // Move to the third integer in the file int newData = 42; fwrite(&newData, sizeof(int), 1, file); // Write a new integer at the current position fclose(file);

Random Access with ftell and fseek for Writing

Use ftell to get the current position in the file, and then use fseek to move to a specific location for writing data.

FILE* file = fopen("data.bin", "rb+"); if (file == NULL) { perror("File opening failed"); return 1; } long position = ftell(file); // Get the current position fseek(file, position + sizeof(int), SEEK_SET); // Move one position forward int newData = 42; fwrite(&newData, sizeof(int), 1, file); // Write a new integer at the new position fclose(file);

When working with random access in C, it's essential to handle potential errors that may occur during file opening and to use proper file modes (like "rb+" for reading and writing) when you need both read and write access. Carefully manage file pointers and positions to avoid data corruption or overwriting unintended portions of the file.

Conclusion

Random access in C programming allows you to read and write data at specific positions within a file, providing flexibility and efficiency when dealing with large or structured data. It is achieved using functions like fseek to move the file pointer to desired positions for both reading and writing operations, offering greater control over file manipulation.