Sequential and Random access file in c programming




Sequential and Random Access File in C



Sequential and Random Access File in C



When working with data in a C program, you often need to interact with files. Two common methods of file access are sequential and random access. In this post, we'll explore both concepts and provide simple examples to help beginners understand how to work with files in C.



Table of Contents



  1. Introduction to File Handling


  2. Sequential Access File

    1. Writing Data to a File

    2. Reading Data from a File




  3. Random Access File

    1. Writing and Reading Randomly



  4. Conclusion



1. Introduction to File Handling



File handling in C allows you to read and write data to external files. It's essential for tasks like data storage, data retrieval, and configuration management. We'll delve into two main file access modes: sequential and random access.



2. Sequential Access File



In sequential file access, data is written or read one item at a time, like reading a book from beginning to end. Let's see how to write and read data sequentially.



2.1 Writing Data to a File



To write data sequentially, follow these steps:




  1. Open the file using fopen() with mode "w" (write mode).

  2. Use fprintf() to write data to the file.

  3. Close the file using fclose().




#include <stdio.h>

int main() {
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
printf("Error opening file.");
return 1;
}

fprintf(file, "Hello, Sequential File!\n");
fprintf(file, "Welcome to C File Handling.\n");

fclose(file);
return 0;
}


2.2 Reading Data from a File



To read data sequentially, follow these steps:




  1. Open the file using fopen() with mode "r" (read mode).

  2. Use fgets() to read data from the file.

  3. Close the file using fclose().




#include <stdio.h>

int main() {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error opening file.");
return 1;
}

char line[100];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}

fclose(file);
return 0;
}


3. Random Access File



In random access file handling, you can read or write data at any position within the file, similar to jumping to specific pages in a book. Here's how it works:



3.1 Writing and Reading Randomly




  1. Open the file using fopen() with mode "rb+" (read and write mode).

  2. Use fseek() to move the file pointer to a specific position.

  3. Use fwrite() to write data, and fread() to read data at the current file pointer position.




#include <stdio.h>

struct Student {
char name[50];
int age;
};

int main() {
FILE *file = fopen("students.dat", "rb+");
if (file == NULL) {
printf("Error opening file.");
return 1;
}

struct Student student;
// Write data at position 1 (second student)
fseek(file, 1 * sizeof(struct Student), SEEK_SET);
strcpy(student.name, "Alice");
student.age = 20;
fwrite(&student, sizeof(struct Student), 1, file);

// Read data from position 0 (first student)
fseek(file, 0 * sizeof(struct Student), SEEK_SET);
fread(&student, sizeof(struct Student), 1, file);
printf("Student: %s, Age: %d\n", student.name, student.age);

fclose(file);
return 0;
}


4. Conclusion



File handling is a crucial aspect of programming in C. In this post, we explored sequential and random access file handling, demonstrated how to write and read data using simple examples, and introduced concepts that are fundamental for working with files in C. Practice these concepts to become more comfortable with file manipulation in your C programs.



Remember, the more you practice, the better you'll get. Happy coding!





Post a Comment

0 Comments