C++读写二进制文件,代码如下:
#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
void main()
{
char *res = (char *)malloc(2000000 * sizeof(char));
int num_res = 0;
// 读入文件
char s = 0;
ifstream inFile("./vcalib_data_2.dat", ios::in | ios::binary); //二进制读方式打开
while (inFile.read((char*)&s, sizeof(char))) {
res[num_res++] = s;
}
printf("resSize=%d\n", num_res);
//写入文件
ofstream outfile("./vcalib_data_2_copy.dat",ios::out|ios::binary);
outfile.write(res, num_res*sizeof(char));
outfile.close();
system("pause");
}