Sequential and random file access

Sequential and random file access

In file handling operation in C programming, any file access can be divided into two types. Which includes sequential file access and random file access methods. So, let’s know both sequential access and random file access methods in file handling in detail in the file program example below.

Sequential and random file access

Sequential Access Methods in C Programming.

Sequential file access in file handling operation means reading or writing data and information in a linear order from the beginning to the end of a file. This is called sequential file access methods. Remember that in most file operations in file handling operation, file access is accessed in sequential file mode by default.

Sequential Access Methods in C Program.

#include <stdio.h>

int main() {

FILE *file = fopen(“test.txt”, “w”); // Open a test file for writing purpose only

if (file == NULL) {

perror(“you get Error while opening a test file for writing purpose”);

 return 1;

 } // below both function Write to the file a character or string

fputs(“\n welcome to, c programming”, file);

fputs(“\n This is a test string file text”, file);

 // Close function used to close the above file fclose(file);

 // Open the test file for reading purpose only

file = fopen(“test.txt”, “r”);

 if (file == NULL) {

perror(“let you face Error while opening file for reading purpose”);

 return 1;

 } // let Read and print the content of the above file char string[150];

 while (fgets(string, 150, file) != NULL) {

 printf(“%s”, string);

 }

// fclose() used to close the file

fclose(file);

return 0;

}

Random Access File Access Methods in C Program.

Random file access methods in C file handling operations allow you to read data or write data to any location in the file without reading the file sequentially. In file handling, when you need to append, read or modify any specific parts of a file without processing the entire file, then you use random file access methods.

Functions for Random Access in C File Operations.

fseek(): // fseek() function lets you move a file pointer to a particular location.

ftell(): // ftell() function lets you move the file pointer to the current position.

rewind(): // rewind() function lets you move the file pointer to the beginning position of the file.

Example of file handling random file access in C.

#include <stdio.h>

int main() {

// Open a test file for writing purpose only

FILE *file = fopen(“test.txt”, “w+”);

 if (file == NULL) {

perror(“you get Error while opening a file”);

 return 1;

 }

// let Write a text or string to the file

fputs(“\n welcome to , c language”, file);

fputs(“\n lets write some text or string test file”, file);

 // here the rewind () used to Move the file pointer to the beginning of the file rewind(file);

 // this function used to overwrite the first 3 characters

fputs(“Hi”, file);

fseek(file, 4, SEEK_SET); // the fseek function used to move the file pointer to position 4

fputs(“tesxt”, file);

 // rewind function Move the file pointer to the beginning location

rewind(file);

// used to Read and print the content of the test file

char string[150];

while (fgets(string, 150, file) != NULL) {

printf(“%s”, string);

}

// fclose function used to Close the file

fclose(file);

return 0;

}

Useful functions used in sequential file access and random file access methods of file handling that provide direct control over the file pointer. This features random access to the file content.