Binary File I/O
Read and write raw bytes.
Binary File I/O is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Text vs Binary
Text mode writes human-readable characters; binary mode writes the raw bytes of objects exactly as they sit in memory. Open with std::ios::binary.
#include <iostream>
#include <fstream>
int main() {
int value = 12345;
std::ofstream out("v.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(&value), sizeof(value));
std::cout << "wrote " << sizeof(value) << " bytes\n";
return 0;
}write Takes Bytes
write(ptr, n) outputs n raw bytes from ptr. You cast the object address to const char*.
#include <iostream>
#include <fstream>
int main() {
double d = 3.14;
std::ofstream out("d.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(&d), sizeof(d));
out.close();
std::cout << "saved a double\n";
return 0;
}read Restores Bytes
read(ptr, n) loads n bytes into the object at ptr, reversing a binary write.
#include <iostream>
#include <fstream>
int main() {
int original = 777;
std::ofstream("i.bin", std::ios::binary).write(reinterpret_cast<const char*>(&original), sizeof(original));
int loaded = 0;
std::ifstream in("i.bin", std::ios::binary);
in.read(reinterpret_cast<char*>(&loaded), sizeof(loaded));
std::cout << loaded << '\n';
return 0;
}Writing a POD Struct
Plain-old-data structs (no pointers, fixed-size members) can be dumped and reloaded as one block of bytes.
#include <iostream>
#include <fstream>
struct Record { int id; double balance; };
int main() {
Record r{1, 99.5};
std::ofstream out("rec.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(&r), sizeof(r));
std::cout << "struct saved\n";
return 0;
}Reading the Struct Back
Reading restores the whole struct in a single read call.
#include <iostream>
#include <fstream>
struct Record { int id; double balance; };
int main() {
Record w{42, 250.0};
std::ofstream("r2.bin", std::ios::binary).write(reinterpret_cast<const char*>(&w), sizeof(w));
Record r{};
std::ifstream in("r2.bin", std::ios::binary);
in.read(reinterpret_cast<char*>(&r), sizeof(r));
std::cout << r.id << ' ' << r.balance << '\n';
return 0;
}Writing an Array
You can write a whole array in one call by passing its total byte size.
#include <iostream>
#include <fstream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
std::ofstream out("arr.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(arr), sizeof(arr));
std::cout << "wrote " << sizeof(arr) << " bytes\n";
return 0;
}Reading the Array Back
Read the same number of bytes into an array of the same size.
#include <iostream>
#include <fstream>
int main() {
int src[5] = {10, 20, 30, 40, 50};
std::ofstream("a2.bin", std::ios::binary).write(reinterpret_cast<const char*>(src), sizeof(src));
int dst[5] = {};
std::ifstream in("a2.bin", std::ios::binary);
in.read(reinterpret_cast<char*>(dst), sizeof(dst));
for (int x : dst) std::cout << x << ' ';
std::cout << '\n';
return 0;
}Seeking Positions
seekg (get) and seekp (put) move the read/write position, enabling random access into the file.
#include <iostream>
#include <fstream>
int main() {
int data[3] = {100, 200, 300};
std::ofstream("seek.bin", std::ios::binary).write(reinterpret_cast<const char*>(data), sizeof(data));
std::ifstream in("seek.bin", std::ios::binary);
in.seekg(sizeof(int)); // skip to second int
int second; in.read(reinterpret_cast<char*>(&second), sizeof(second));
std::cout << second << '\n';
return 0;
}Telling the Position
tellg and tellp report the current position, useful for measuring file size or offsets.
#include <iostream>
#include <fstream>
int main() {
int data[4] = {1, 2, 3, 4};
std::ofstream("tell.bin", std::ios::binary).write(reinterpret_cast<const char*>(data), sizeof(data));
std::ifstream in("tell.bin", std::ios::binary);
in.seekg(0, std::ios::end);
std::cout << "file size: " << in.tellg() << " bytes\n";
return 0;
}Why Not Pointers
Never binary-dump objects containing pointers or std::string: you would save the pointer address, not the data it points to. Serialize the contents instead.
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string s = "hello";
std::ofstream out("str.bin", std::ios::binary);
std::size_t len = s.size();
out.write(reinterpret_cast<const char*>(&len), sizeof(len));
out.write(s.data(), len); // write the chars, not the object
std::cout << "serialized " << len << " chars\n";
return 0;
}Portability Caveats
Binary layouts depend on endianness and type sizes. Files written on one platform may not read correctly on another, so define a fixed format for sharing.
#include <iostream>
#include <cstdint>
int main() {
std::int32_t fixed = 1; // fixed-width type for portability
std::cout << "sizeof int32_t = " << sizeof(fixed) << '\n';
return 0;
}Quick Check
Test your understanding of binary I/O.
Recap
You learned binary file I/O:
- open with
std::ios::binary; usewrite/readwithreinterpret_cast<char*> seekg/seekpandtellg/tellpgive random access- never dump pointers or strings directly; serialize their contents, and beware endianness
Next, you'll learn to check stream state and handle errors.
Frequently asked questions
Is the “Binary File I/O” lesson free?
Yes — the full text of “Binary File I/O” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Binary File I/O”?
Read and write raw bytes. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Binary File I/O” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.