Binary File Handling

Storing structured data efficiently using binary files.

Binary File Handling

Binary File Handling का अर्थ computer program की सहायता से binary files को create (बनाना), open (खोलना), read (पढ़ना), write (लिखना), append (नई information जोड़ना) और close (बंद करना) है। Binary files में data सामान्य text की तरह characters के रूप में नहीं, बल्कि binary form (बाइनरी रूप) में store किया जाता है।

Binary File Handling: Open → Read/Write Binary Data → Close

What is a Binary File?

Binary File ऐसी file होती है जिसमें data computer के binary format में store किया जाता है। इसमें data को सामान्य text editor में पढ़ना आसान नहीं होता। Binary files का उपयोग data को compact और structured form में store करने के लिए किया जा सकता है।

उदाहरण के लिए, student records, images, audio files और कुछ application-specific data binary form में store किए जा सकते हैं।

Binary File vs Text File

Text File Binary File
Data readable text form में store होता है। Data binary form में store होता है।
Text editor से आसानी से पढ़ा जा सकता है। Text editor में data सामान्यतः readable नहीं होता।
Characters और text data के लिए उपयुक्त है। Structured और binary data के लिए उपयोगी है।
Formatting के कारण file size अपेक्षाकृत अधिक हो सकता है। Data को compact form में store किया जा सकता है।

Binary File Handling in C++

C++ में binary file handling के लिए <fstream> header file का उपयोग किया जाता है। Binary data को read और write करने के लिए मुख्य रूप से read() और write() member functions का उपयोग किया जाता है।

#include <fstream>

Opening a Binary File

Binary file को open करते समय ios::binary mode का उपयोग किया जाता है। यह stream को binary mode में file access करने के लिए बताता है।

उदाहरण:

ofstream file("student.dat", ios::binary);

यहाँ student.dat file binary mode में writing के लिए open की गई है।

Writing Binary Data

Binary file में data write करने के लिए write() function का उपयोग किया जाता है। यह function memory में मौजूद bytes को file में write करता है।

write() function का सामान्य syntax:

file.write((char*)&variable, sizeof(variable));

यहाँ sizeof() operator variable के size को bytes में प्राप्त करता है और (char*)& object के memory address को character pointer के रूप में treat करने में सहायता करता है।

Example: Writing Data to a Binary File

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int number = 100;

    ofstream file("number.dat", ios::binary);

    file.write((char*)&number, sizeof(number));

    file.close();

    cout << "Data written successfully";

    return 0;
}
Output:
Data written successfully

इस program में integer value 100 को binary form में number.dat file में store किया गया है।

Reading Binary Data

Binary file से data पढ़ने के लिए read() function का उपयोग किया जाता है। यह file से specified number of bytes को memory में read करता है।

read() function का सामान्य syntax:

file.read((char*)&variable, sizeof(variable));

Example: Reading Data from a Binary File

मान लीजिए number.dat file में पहले से integer value 100 stored है। उसे read करने का program इस प्रकार है:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int number;

    ifstream file("number.dat", ios::binary);

    file.read((char*)&number, sizeof(number));

    file.close();

    cout << "Number: " << number;

    return 0;
}
Output:
Number: 100

Writing and Reading a Structure

Binary file handling का एक महत्वपूर्ण उपयोग structure records को file में store करना है। Structure के object को write() function की सहायता से binary file में write किया जा सकता है और read() function की सहायता से वापस read किया जा सकता है।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s = {101, "Rahul", 87.5};

    ofstream file("student.dat", ios::binary);

    file.write((char*)&s, sizeof(s));

    file.close();

    cout << "Student record saved";

    return 0;
}
Output:
Student record saved

इस program में Student structure का object binary file में store किया गया है।

Reading a Structure from Binary File

Binary file में stored structure record को read() function की सहायता से वापस object में read किया जा सकता है।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s;

    ifstream file("student.dat", ios::binary);

    file.read((char*)&s, sizeof(s));

    file.close();

    cout << "Roll: " << s.roll << endl;
    cout << "Name: " << s.name << endl;
    cout << "Marks: " << s.marks;

    return 0;
}
Output:
Roll: 101
Name: Rahul
Marks: 87.5

File Opening Modes for Binary Files

Binary file के साथ अलग-अलग operations करने के लिए file opening modes का उपयोग किया जाता है। Binary mode को अन्य modes के साथ combine किया जा सकता है।

Mode Purpose
ios::binary File को binary mode में open करना
ios::in File से data read करना
ios::out File में data write करना
ios::app File के end में data जोड़ना
ios::ate File open होने के बाद position को end पर ले जाना

Combining File Modes

एक से अधिक modes को bitwise OR operator | की सहायता से combine किया जा सकता है।

fstream file("student.dat", ios::in | ios::out | ios::binary);

यह file को reading और writing दोनों के लिए binary mode में open करता है।

Appending Binary Records

यदि existing binary file के अंत में नया record जोड़ना हो, तो ios::app और ios::binary को combine किया जा सकता है।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s = {102, "Amit", 91.0};

    ofstream file("student.dat", ios::binary | ios::app);

    file.write((char*)&s, sizeof(s));

    file.close();

    cout << "Record appended successfully";

    return 0;
}
Output:
Record appended successfully

Reading Multiple Binary Records

यदि binary file में कई records stored हों, तो while loop के साथ read() function का उपयोग करके सभी records को एक-एक करके पढ़ा जा सकता है।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s;

    ifstream file("student.dat", ios::binary);

    while (file.read((char*)&s, sizeof(s)))
    {
        cout << "Roll: " << s.roll << endl;
        cout << "Name: " << s.name << endl;
        cout << "Marks: " << s.marks << endl;
    }

    file.close();

    return 0;
}
Output:
Roll: 101
Name: Rahul
Marks: 87.5
Roll: 102
Name: Amit
Marks: 91

यहाँ read() function successful होने तक loop चलता है और file के सभी student records को क्रम से display करता है।

Checking End of File

Binary file से records पढ़ते समय अक्सर read() function को while condition में directly use किया जाता है। इससे केवल वही record process होता है जिसे successfully read किया गया हो।

while (file.read((char*)&s, sizeof(s)))
{
    // Process the record
}
Important: File reading के लिए while(file.read(...)) pattern उपयोग करना अधिक उचित है क्योंकि इससे unsuccessful read के बाद invalid record process नहीं होता।

read() and write() Functions

Function Purpose Operation
write() Binary data को file में लिखना Write
read() Binary data को file से पढ़ना Read

Text File and Binary File Handling

Text और binary file handling दोनों का उद्देश्य data को file में store करना है, लेकिन दोनों में data representation अलग होती है।

// Text file
ofstream file1("data.txt");
file1 << 100;
file1.close();

// Binary file
ofstream file2("data.dat", ios::binary);
int n = 100;
file2.write((char*)&n, sizeof(n));
file2.close();

पहले example में 100 को text representation में लिखा जाता है, जबकि दूसरे example में integer की binary representation के bytes write किए जाते हैं।

Advantages of Binary File Handling

  • Data को compact form में store किया जा सकता है।
  • Structured records को efficiently store किया जा सकता है।
  • read() और write() की सहायता से fixed-size records को directly transfer किया जा सकता है।
  • कुछ applications में binary storage text storage की तुलना में अधिक efficient हो सकता है।

Limitations of Binary Files

  • Binary file को सामान्य text editor में आसानी से समझा नहीं जा सकता।
  • Binary data का format program और platform पर निर्भर हो सकता है।
  • Different systems के बीच binary data share करते समय compatibility issues हो सकते हैं।
  • Data को manually edit करना कठिन होता है।

Important Points

  • C++ में binary file handling के लिए <fstream> header file का उपयोग किया जाता है।
  • Binary file को binary mode में open करने के लिए ios::binary का उपयोग किया जाता है।
  • write() function binary data को file में write करता है।
  • read() function binary data को file से read करता है।
  • sizeof() operator data के size को bytes में प्राप्त करने में उपयोगी है।
  • Structures के records को binary files में store किया जा सकता है।
  • ios::app | ios::binary का उपयोग existing binary file के end में records जोड़ने के लिए किया जा सकता है।
  • Multiple records पढ़ने के लिए while(file.read(...)) का उपयोग किया जा सकता है।
  • File operation पूरा होने के बाद close() function का उपयोग करना चाहिए।

Board Focus

Exam के लिए याद रखें:
<fstream> → File Handling Header File
ios::binary → Binary Mode
write() → Binary Data Write करना
read() → Binary Data Read करना
sizeof() → Data का Size in Bytes
ios::app → End में Record Add करना
close() → File Close करना

Most Important: Binary file में data को binary form में store किया जाता है और C++ में binary data के लिए मुख्य रूप से read() तथा write() functions का उपयोग किया जाता है।

Board Important Questions

Very Short Answer Questions

Q1. Binary File क्या है?

Answer: Binary File वह file है जिसमें data binary form में store किया जाता है।

Q2. C++ में binary file handling के लिए कौन-सी header file उपयोग होती है?

Answer: <fstream> header file का उपयोग किया जाता है।

Q3. Binary file को binary mode में open करने के लिए कौन-सा mode उपयोग होता है?

Answer: ios::binary mode का उपयोग होता है।

Q4. Binary file में data write करने के लिए कौन-सा function उपयोग होता है?

Answer: write() function का उपयोग होता है।

Q5. Binary file से data read करने के लिए कौन-सा function उपयोग होता है?

Answer: read() function का उपयोग होता है।

Q6. sizeof() operator का क्या उपयोग है?

Answer: sizeof() operator किसी data type या variable का size bytes में प्राप्त करने के लिए उपयोग होता है।

Q7. Binary file में existing records के अंत में नया record जोड़ने के लिए कौन-सा mode उपयोग किया जाता है?

Answer: ios::app mode का उपयोग किया जाता है।

Short Answer Questions

Q8. Binary File Handling क्या है?

Answer: Binary File Handling का अर्थ program की सहायता से binary files को create, open, read, write, append और close करना है। इसमें data binary form में store और access किया जाता है।

Q9. Text File और Binary File में अंतर बताइए।

Text File Binary File
Data readable text form में store होता है। Data binary form में store होता है।
Text editor से पढ़ना आसान होता है। Text editor से पढ़ना सामान्यतः कठिन होता है।
Text data के लिए उपयुक्त है। Structured binary data के लिए उपयोगी है।

Q10. read() और write() functions को समझाइए।

Answer: write() function binary data को memory से file में write करने के लिए और read() function binary data को file से memory में read करने के लिए उपयोग किया जाता है। दोनों functions के साथ सामान्यतः data का address और size in bytes दिया जाता है।

Q11. ios::binary का क्या महत्व है?

Answer: ios::binary stream को binary mode में file access करने के लिए उपयोग होता है। यह binary data को उसके byte representation के अनुसार read और write करने में सहायता करता है।

Q12. Binary files में structures का उपयोग क्यों किया जाता है?

Answer: Structures में related data members को एक record के रूप में group किया जा सकता है। Binary file में structure objects को read() और write() functions की सहायता से record के रूप में store और retrieve किया जा सकता है।

Long Answer Questions

Q13. C++ में binary file में data write करने का program लिखिए।

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int n = 250;

    ofstream file("data.dat", ios::binary);

    file.write((char*)&n, sizeof(n));

    file.close();

    cout << "Data written successfully";

    return 0;
}
Output:
Data written successfully

Q14. Binary file से data read करने का C++ program लिखिए।

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int n;

    ifstream file("data.dat", ios::binary);

    file.read((char*)&n, sizeof(n));

    file.close();

    cout << "Number: " << n;

    return 0;
}
Output:
Number: 250

Q15. Structure के record को binary file में write करने का program लिखिए।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s = {1, "Aman", 92.5};

    ofstream file("student.dat", ios::binary);

    file.write((char*)&s, sizeof(s));

    file.close();

    cout << "Record saved";

    return 0;
}
Output:
Record saved

Q16. Binary file से multiple records पढ़ने का C++ program लिखिए।

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
    int roll;
    char name[20];
    float marks;
};

int main()
{
    Student s;

    ifstream file("student.dat", ios::binary);

    while (file.read((char*)&s, sizeof(s)))
    {
        cout << s.roll << " "
             << s.name << " "
             << s.marks << endl;
    }

    file.close();

    return 0;
}
Output:
1 Aman 92.5
2 Ravi 88

Quick Revision

  • Binary File: Binary form में data store करने वाली file
  • ios::binary: Binary mode
  • write(): Binary data को file में write करना
  • read(): Binary data को file से read करना
  • sizeof(): Data का size bytes में प्राप्त करना
  • ios::app: Existing binary file के end में data जोड़ना
  • Structure: Records को binary file में store करने के लिए उपयोगी
One-Line Revision: C++ में Binary File Handling के द्वारा ios::binary mode का उपयोग करके binary data को read() और write() functions की सहायता से file में store और retrieve किया जाता है।

Practice Questions

  1. Binary File क्या है?
  2. Binary File और Text File में अंतर बताइए।
  3. C++ में binary file handling के लिए कौन-सी header file उपयोग होती है?
  4. ios::binary का क्या उपयोग है?
  5. write() function क्या करता है?
  6. read() function क्या करता है?
  7. sizeof() operator का क्या उपयोग है?
  8. Binary file में structure record कैसे store किया जाता है?
  9. ios::app और ios::binary को एक साथ क्यों उपयोग किया जा सकता है?
  10. Binary file से multiple records पढ़ने का program लिखिए।
  11. Binary file में integer value write करने का program लिखिए।
  12. Binary file से integer value read करने का program लिखिए।
  13. Structure record को binary file में write करने का C++ program लिखिए।
  14. Structure record को binary file से read करने का C++ program लिखिए।
  15. Text File Handling और Binary File Handling की तुलना कीजिए।
Lesson 13 of 37
On This Page