#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "ReadText.h"
using namespace std;
const string FILE_NAME = "TopicIinBasic.txt";
template<typename T>
char * as_bytes( T &inType );
int main()
{
vector<int> vec(2, 2);
vector<int> receive(2);
fstream write( "myFile.dat", ios::out | ios::binary );
write.write( as_bytes(vec[0]), sizeof(vec[0] * 2) );
write.close();
fstream read( "myFile.dat", ios::in | ios::binary );
read.read( as_bytes(receive[0]), sizeof(vec[0] * 2) );
cout << receive[0] << ' ' << receive[1] << endl;
return 0;
}
template<typename T>
char * as_bytes( T &inType )
{
void* addr = &inType;
return static_cast<char*>(addr);
}
我首先将内容写入vec
二进制文件。然后关闭文件。然后以阅读模式再次打开它。然后我尝试将二进制文件的内容放入receive
. 但是当我显示receive
的内容时,输出是2 0
,而不是2 2
。为什么会这样?
谢谢。