Binary File Handling in C Using fread() and fwrite() with Examples
Binary File Handling in C Language
In C language, files are mainly handled in two modes: Text Mode and Binary Mode. A binary file stores data in the form of bytes. Unlike text files, binary files are generally used to store data in a format that can be directly read and written by a program, such as integers, floating-point values and structures.
सी लैंग्वेज में फाइलों को मुख्य रूप से दो मोड में हैंडल किया जाता है: Text Mode और Binary Mode। Binary File डेटा को Bytes के रूप में स्टोर करती है। Text Files के विपरीत, Binary Files का उपयोग सामान्यतः डेटा को ऐसे फॉर्मेट में स्टोर करने के लिए किया जाता है जिसे प्रोग्राम सीधे पढ़ और लिख सकता है, जैसे Integers, Floating-Point Values और Structures।
Binary file operations are performed using fread() and fwrite() functions. These functions are declared in the standard input/output header file stdio.h.
Binary File पर Read और Write Operations करने के लिए fread() और fwrite() फंक्शन्स का उपयोग किया जाता है। ये दोनों फंक्शन्स stdio.h Header File में परिभाषित होते हैं।
1. fwrite() Function in C
The fwrite() function is used to write data into a binary file. It writes data from memory to a file in the form of blocks or items.
fwrite() फंक्शन का उपयोग Binary File में डेटा लिखने के लिए किया जाता है। यह Memory से डेटा लेकर उसे File में Blocks या Items के रूप में लिखता है।
Syntax:
fwrite(pointer, size, count, file_pointer);Parameters:
pointer– Address of the data to be written.size– Size of each item in bytes.count– Number of items to be written.file_pointer– Pointer to the opened file.
Return Value:
The fwrite() function returns the number of items successfully written. If fewer items are written than requested, an error may have occurred.
fwrite() फंक्शन सफलतापूर्वक लिखे गए Items की संख्या Return करता है। यदि लिखे गए Items की संख्या निर्धारित संख्या से कम है, तो Writing में Error हो सकता है।
2. fread() Function in C
The fread() function is used to read data from a binary file into memory. It reads a specified number of items from the file.
fread() फंक्शन का उपयोग Binary File से डेटा पढ़कर Memory में स्टोर करने के लिए किया जाता है। यह फाइल से निर्धारित संख्या में Items पढ़ता है।
Syntax:
fread(pointer, size, count, file_pointer);Parameters:
pointer– Address of the memory location where data will be stored.size– Size of each item in bytes.count– Maximum number of items to be read.file_pointer– Pointer to the opened file.
Return Value:
The fread() function returns the number of complete items successfully read. This value may be less than the requested count if the end of the file is reached or a reading error occurs.
fread() फंक्शन सफलतापूर्वक पढ़े गए Complete Items की संख्या Return करता है। यदि फाइल समाप्त हो जाती है या Reading Error होता है, तो यह निर्धारित संख्या से कम Items Return कर सकता है।
3. Difference Between Text File and Binary File
| Text File | Binary File |
|---|---|
| Data is stored as text characters. | Data is stored as bytes. |
| Human-readable in a text editor in ordinary cases. | Usually not directly human-readable. |
Commonly uses fprintf(), fscanf(), fgets() and fputs(). | Commonly uses fread() and fwrite(). |
Opened using modes such as "r" and "w". | Opened using modes such as "rb" and "wb". |
| Text stream may perform character transformations. | Binary stream does not perform text-mode transformations. |
Text File में डेटा सामान्यतः पढ़े जा सकने वाले Characters के रूप में स्टोर होता है, जबकि Binary File में डेटा Bytes के रूप में स्टोर होता है। Text File और Binary File के लिए अलग-अलग File Opening Modes का उपयोग किया जाता है।
4. C Program to Write and Read Data Using fwrite() and fread()
The following program accepts student details from the user, writes them into a binary file named student.dat using fwrite(), and then reads the stored record using fread().
निम्नलिखित प्रोग्राम User से Student की Details प्राप्त करता है और fwrite() की सहायता से उन्हें student.dat नाम की Binary File में स्टोर करता है। इसके बाद fread() की सहायता से उस Record को पढ़कर प्रदर्शित करता है।
Program:
#include <stdio.h>struct student{int roll;char name[30];float marks;};int main(void){FILE *fp;struct student s1, s2;size_t written, read_count;printf("Enter student roll number: ");if (scanf("%d", &s1.roll) != 1){printf("Invalid roll number.\n");return 1;}printf("Enter student name: ");if (scanf(" %29[^\n]", s1.name) != 1){printf("Invalid name.\n");return 1;}printf("Enter student marks: ");if (scanf("%f", &s1.marks) != 1){printf("Invalid marks.\n");return 1;}/* Open binary file in write mode */fp = fopen("student.dat", "wb");if (fp == NULL){printf("Unable to create file.\n");return 1;}/* Write student record */written = fwrite(&s1, sizeof(struct student), 1, fp);if (written != 1){printf("Error writing to file.\n");fclose(fp);return 1;}fclose(fp);/* Open binary file in read mode */fp = fopen("student.dat", "rb");if (fp == NULL){printf("Unable to open file.\n");return 1;}/* Read student record */read_count = fread(&s2, sizeof(struct student), 1, fp);if (read_count != 1){printf("Error reading from file.\n");fclose(fp);return 1;}printf("\nStudent Record Read From File\n");printf("Roll Number: %d\n", s2.roll);printf("Name: %s\n", s2.name);printf("Marks: %.2f\n", s2.marks);fclose(fp);return 0;}
Output
Enter student roll number: 101Enter student name: Ramesh KumarEnter student marks: 85.50Student Record Read From FileRoll Number: 101Name: Ramesh KumarMarks: 85.50
Explanation:
struct studentdefines a structure containing roll number, name and marks. यह Student की जानकारी को एक Structure में स्टोर करता है।fopen("student.dat", "wb")opens a binary file in write mode. यदि फाइल मौजूद नहीं है, तो नई फाइल बनाई जाती है। यदि पहले से मौजूद है, तो उसका पुराना Content हट जाता है।fwrite(&s1, sizeof(struct student), 1, fp)writes one complete student record into the binary file. यह Structure के पूरे डेटा को एक Item के रूप में फाइल में लिखता है।fopen("student.dat", "rb")opens the binary file in read mode. यह पहले से मौजूद Binary File को पढ़ने के लिए खोलता है।fread(&s2, sizeof(struct student), 1, fp)reads one student record from the file into structure variables2. यह फाइल से एक Student Record पढ़करs2में स्टोर करता है।fclose(fp)closes the opened file. यह File को बंद करता है।
5. Important Points About Binary File Handling
Use
"rb"to open a binary file for reading. Binary File को पढ़ने के लिए"rb"Mode का उपयोग करें।Use
"wb"to open a binary file for writing. Binary File में डेटा लिखने के लिए"wb"Mode का उपयोग करें। ध्यान रखें कि यह पुरानी File Content को हटाकर नई Writing शुरू करता है।Use
"ab"to append data to an existing binary file. मौजूदा Binary File के अंत में नया डेटा जोड़ने के लिए"ab"Mode का उपयोग करें।fread()andfwrite()work with blocks of data. ये Functions एक बार में एक या अधिक Items को Read या Write कर सकते हैं।The return value of
fread()andfwrite()represents the number of complete items processed, not the total number of bytes. इन Functions का Return Value सफलतापूर्वक Process किए गए Items की संख्या बताता है, Bytes की कुल संख्या नहीं।Binary files containing structures may not be portable between different compilers or computer systems because of differences in data representation, structure padding and byte order. इसलिए एक सिस्टम पर बनाई गई Structure-Based Binary File दूसरे सिस्टम पर हमेशा उसी प्रकार काम करे, यह आवश्यक नहीं है।
The example uses a simple structure for learning purposes. For long-term storage or exchanging data between different systems, a defined portable file format is generally preferable to writing raw structure memory.
यह उदाहरण सीखने के उद्देश्य से एक Simple Structure का उपयोग करता है। लंबे समय तक डेटा सुरक्षित रखने या अलग-अलग Systems के बीच डेटा का आदान-प्रदान करने के लिए Raw Structure Memory के बजाय एक निर्धारित Portable File Format का उपयोग करना बेहतर होता है।
Comments
Post a Comment