Writing and reading binary files

Good day,

i am having a problem with writing and reading binary files. i searched on google and this forum but i can't quite get the information i need. i am a beginner.

so i got this code for creating a simple binary file.
1
2
3
4
ofstream outfile("binary.txt", ofstream::binary);
string text = "Hello cplusplus forum!";
outfile.write((char*)&text, text.size());
outfile.close();

when i open this file it is binary but whenever i try to read this file back into the program with this code:
1
2
3
4
5
6
7
char* buffer = (char*)malloc(sizeof(string));
ifstream infile("binary.txt", ifstream::binary);
infile.read(buffer, sizeof(string));
string* elem = (string*)buffer;
cout << *elem << '\n';
infile.close();
main();

the program and visual studio crashes.

i created this code with bits and pieces i searched for on the internet.
i would like to know how writing and reading binary in c++ actually works.

any help will be appriciated!


With friendly greetings,

krreisys.
Last edited on
There are a few problems here in the first part:
1
2
3
4
    ofstream outfile("binary.txt", ofstream::binary);
    string text = "Hello cplusplus forum!";
    outfile.write((char*)&text, text.size());
    outfile.close();

The code will probably run with no problems. But what is contained in the file binary.txt? For this sort of job, a hex editor is useful. On windows you may try Hxd
https://mh-nexus.de/en/hxd/

What I see on my system is something like this:
TA>.$ÀÂw..>.LXH.èUH.@?
together with the corresponding hex values.

It is pretty meaningless. That's because the string object may not contain the text of the string, instead it contains a pointer to the string held on the heap, and possibly some other information.

The first step is to write the actual text content of the string itself.
You can do that by replacing line 3
 
    outfile.write((char*)&text, text.size());

with this:
 
    outfile.write( text.c_str(), text.size());

Now if you look again at the contents of the file, it should make more sense.

But that still leaves a problem. How will you read it back in? It's easy enough if that's all the file contains, just read until end of file. But what if you want to read just the original string, no more, no less?

Well, then you could first store the size in the file. Like so:
1
2
    unsigned int size = text.size();
    outfile.write(reinterpret_cast<char *>(&size), sizeof(size)  );


To read the file, you do the steps in reverse.

Complete example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
    
int main()
{   

    string text = "Hello cplusplus forum!";

    ofstream outfile("binary.txt", ofstream::binary);
    unsigned int size = text.size();                                 // get the size
    outfile.write(reinterpret_cast<char *>(&size), sizeof(size)  );  // write it to the file
    outfile.write( text.c_str(), text.size() );                      // write the actual text
    outfile.close();


    //----------------------------------------

    ifstream infile("binary.txt", ifstream::binary);
    
    // read the size
    size = 0;        
    infile.read(reinterpret_cast<char *>(&size), sizeof(size)  );
    
    // Allocate a string, make it large enough to hold the input
    string buffer;
    buffer.resize(size);
    
    // read the text into the string
    infile.read(&buffer[0],  buffer.size() );
    infile.close();    
    
    cout << "buffer = \n" << buffer << '\n';
    
}

Last edited on
It worked thx!
Topic archived. No new replies allowed.