Block I/O Functions in C

Introduction

C language में जब हमें file से large amount of data (blocks में) read या write करना होता है, तब हम Block I/O Functions का उपयोग करते हैं।

  • Character I/O → एक-एक character
  • String I/O → एक line
  • Block I/O → एक साथ पूरा block (multiple bytes / structures)

यह binary files और large data handling में बहुत useful होता है।

Block I/O Functions

FunctionUse
fwrite()File में block data लिखने के लिए
fread()File से block data पढ़ने के लिए

1. Writing using fwrite()

Syntax

fwrite(pointer, size, count, file_pointer);

Parameters

  • pointer → data का address
  • size → एक element का size (bytes में)
  • count → कितने elements लिखने हैं
  • file_pointer → file pointer

Example Program:

#include <stdio.h>

int main() {

FILE *fp;
int arr[5] = {10, 20, 30, 40, 50};
fp = fopen("data.bin", "wb");

if (fp == NULL) {
printf("Error opening file\n");
return 1;
}

fwrite(arr, sizeof(int), 5, fp);

fclose(fp);

printf("Data written successfully\n");

return 0;
}

Program Explanation

  1. integer array बनाया गया
  2. file "data.bin" binary write mode (wb) में open की गई
  3. fwrite() का use:
fwrite(arr, sizeof(int), 5, fp);

इसका मतलब:

  • array का address → arr
  • size of each element → sizeof(int)
  • total elements → 5
  1. पूरा array एक साथ file में store हो गया

Output:

Data written successfully

2. Reading using fread()

Syntax

fread(pointer, size, count, file_pointer);

Example Program:

#include <stdio.h>

int main() {

FILE *fp;
int arr[5];

fp = fopen("data.bin", "rb");

if (fp == NULL) {
printf("File not found\n");
return 1;
}

fread(arr, sizeof(int), 5, fp);

printf("Data from file:\n");

for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

fclose(fp);

return 0;
}

Program Explanation

  1. file "data.bin" binary read mode (rb) में open की गई
  2. fread() से पूरा block read किया गया
fread(arr, sizeof(int), 5, fp);

यह पूरा array memory में load करता है

  1. loop के द्वारा data print किया गया

Output:

Data from file:
10 20 30 40 50

3. Example with Structure (Important for Exam)

#include <stdio.h>

struct Student {
int id;
float marks;
};

int main() {
FILE *fp;
struct Student s1 = {1, 85.5};

fp = fopen("student.bin", "wb");
fwrite(&s1, sizeof(s1), 1, fp);
fclose(fp);

struct Student s2;

fp = fopen("student.bin", "rb");
fread(&s2, sizeof(s2), 1, fp);
fclose(fp);

printf("ID = %d\nMarks = %.2f\n", s2.id, s2.marks);

return 0;
}

Program Explanation

  • structure Student define किया गया
  • fwrite() से पूरा structure file में store किया गया
  • fread() से वापस read किया गया
  • data print किया गया

Output:

ID = 1
Marks = 85.50

Important Points

  • fwrite() → block write
  • fread() → block read
  • binary mode (wb, rb) use करना चाहिए
  • sizeof() बहुत important है

Advantages

1. Fast

Large data एक साथ process होता है

2. Efficient

Memory और time दोनों बचता है

3. Structure Handling

Structures directly store कर सकते हैं

Limitations

  • Human readable नहीं होता (binary data)
  • debugging मुश्किल हो सकती है

Exam / Practical Tips

  • fwrite() और fread() बहुत important हैं
  • structure program जरूर आता है
  • binary modes (rb, wb) याद रखें

Conclusion

Block I/O functions (fwrite() और fread()) C file handling का advanced concept हैं, जो large data और structures को efficiently handle करने में मदद करते हैं।

यह real-world applications जैसे database files, records storage और binary data handling में बहुत उपयोगी होते हैं।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top