← Back to C++ Course | Chapter 15: File I/O | Lesson 4 of 6

C++ की Binary Files

एक binary file raw data exactly वैसे ही store करती है जैसे computer इसे holds करता है, describe करने की बजाय एक photo लेने की तरह। यह compact है लेकिन human-readable नहीं।
Syntax
cpp
std::ofstream out("filename.bin", std::ios::binary);
out.write(reinterpret_cast<char*>(&variable), sizeof(variable));

std::ifstream in("filename.bin", std::ios::binary);
in.read(reinterpret_cast<char*>(&variable), sizeof(variable));

Binary Data Write करना

Binary mode एक variable की raw in-memory byte representation सीधे disk पर store करता है, पहले इसे human-readable text में convert करने की बजाय।

ios::binary flag के साथ एक stream open करना और एक pointer और byte count के साथ write() call करना data को exactly वैसे save करता है जैसे यह memory में exist करता है -- text output से faster और ज़्यादा compact, लेकिन human-readable नहीं।

उदाहरण: Writing Binary Data

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 42
	int value = 42;
	std::ofstream file("data.bin", std::ios::binary);
	file.write(reinterpret_cast<char *>(&value), sizeof(value));
	file.close();
	// Print to the console with cout
	std::cout << "Binary data written" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Binary Data Read करना

read() write() का binary counterpart है -- यह file से दिए गए bytes count को सीधे एक variable की memory में copy करता है।

क्योंकि read() एक char* expect करता है, आपको पहले अपने variable के address को उस type में reinterpret_cast करना ज़रूरी है, क्योंकि C++ implicitly एक int* या struct pointer को char* में convert नहीं करता।

उदाहरण: Reading Binary Data

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 42
	int value = 42;
	std::ofstream out("data.bin", std::ios::binary);
	out.write(reinterpret_cast<char *>(&value), sizeof(value));
	out.close();

	int readValue;
	std::ifstream in("data.bin", std::ios::binary);
	in.read(reinterpret_cast<char *>(&readValue), sizeof(readValue));
	// Print to the console with cout
	std::cout << readValue << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Binary बनाम Text Mode

Text mode I/O के दौरान कुछ characters translate कर सकता है (जैसे operating systems के बीच newline conventions convert करना), जो int या struct जैसे non-text data को corrupt कर देगा।

Binary mode ऐसा सारा translation disable करता है, कुछ भी add या change किए बिना exact bytes transfer करते हुए।

उदाहरण: Binary vs Text Mode

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	// Declare pi and set it to 3.14159
	double pi = 3.14159;
	std::ofstream file("data.bin", std::ios::binary);
	file.write(reinterpret_cast<char *>(&pi), sizeof(pi));
	file.close();
	// Print to the console with cout
	std::cout << "Written in binary mode, no text translation" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structs के Arrays Write/Read करना

क्योंकि write()/read() element-by-element conversions की बजाय एक byte count से काम करते हैं, आप sizeof(struct) * count इस्तेमाल करते हुए एक call में structs का पूरा array pass कर सकते हैं -- यह single bulk operation एक बार में एक field loop करके लिखने से dramatically तेज़ है।

उदाहरण: Writing/Reading Arrays of Structs

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Define a structure type named Point
struct Point { int x, y; };

// Program execution starts in main()
int main() {
	Point points[2] = {{1, 2}, {3, 4}};
	std::ofstream file("data.bin", std::ios::binary);
	file.write(reinterpret_cast<char *>(points), sizeof(Point) * 2);
	file.close();
	// Print to the console with cout
	std::cout << "Wrote array of structs in one call" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Binary Files में Random Access

क्योंकि एक binary file में हर record एक fixed, known संख्या bytes (sizeof(RecordType)) occupy करती है, आप इससे पहले हर record पढ़ने की बजाय seekg(N * sizeof(RecordType)) से सीधे Nth record पर jump कर सकते हैं -- यही चीज़ specific records तक random access को practical बनाती है।

उदाहरण: Random Access in Binary Files

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Define a structure type named Record
struct Record { int id; };

// Program execution starts in main()
int main() {
	Record records[3] = {{1}, {2}, {3}};
	std::ofstream out("data.bin", std::ios::binary);
	out.write(reinterpret_cast<char *>(records), sizeof(Record) * 3);
	out.close();

	std::ifstream in("data.bin", std::ios::binary);
	in.seekg(1 * sizeof(Record));
	Record r;
	in.read(reinterpret_cast<char *>(&r), sizeof(Record));
	// Print to the console with cout
	std::cout << r.id << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ios::binary के बिना एक binary file open करना, इसलिए Windows पर text translation data corrupt कर सकता है।
  2. pointers वाले objects लिखना, जो data की बजाय addresses store करता है।
  3. लिखे गए bytes से अलग संख्या bytes पढ़ना।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.