Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Text files store human-readable ASCII/UTF-8 strings. While easy to read in a text editor, converting numeric types to text strings incurs formatting overhead and consumes extra space. Binary files dump exact in-memory bytes directly to disk, providing maximum I/O throughput.
To read or write binary memory, open files with the flag std::ios::binary and use:
stream.write(reinterpret_cast<const char*>(&obj), sizeof(obj));stream.read(reinterpret_cast<char*>(&obj), sizeof(obj));Only Plain Old Data (POD) structs or Trivially Copyable types can be serialized using raw byte dumps. Classes containing pointers, std::string, or virtual tables (vptr) cannot be dumped with reinterpret_cast because pointer addresses become invalid once the process restarts.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| File I/O with std::ifstream and std::ofstream | String Streams with std::stringstream for Parsing |
Test your understanding of this lesson with 1 questions. Each question has one correct answer.