0

I have an array of bytes as integers in base 256. So it looks like [0, 120, 255, 30, 21]. Each number represents a byte, so like 0 = 0000000, 1 = 0000001, 2 = 00000010, etc...

How can I write this array to a file? I am really lost I don't know where to begin with this.

4

3 回答 3

2

Here is a simple example:

#include <fstream.h>

char buffer[100] = {0, 120, 255, 30, 21, ... };
ofstream myFile ("data.bin", ios::out | ios::binary);
myFile.write (buffer, 100);
于 2013-08-21T05:26:24.250 回答
0

You can open the file in binary mode in C++ with fstream like this http://www.cplusplus.com/forum/general/11272/.

Or open a file in C compatible fashion,

fopen("test.bin", "bw+");
于 2013-08-21T05:22:39.790 回答
0

You want something like:

ofstream my_bin_file;
my_bin_file.open("filename.bin", ios::out | ios::binary);
// some loop to get your int byte's
    my_bin_file << byte;
my_bin_file.close();
于 2013-08-21T05:27:38.777 回答