二进制文件读写
#include<iostream>#include<string>
#include<fstream>
using namespace std;
int main()
{
string s;
ifstream fin("Rata.txt"); // 内容为 3 1 2
ofstream fout;
if(fin.fail())
cout<<"error";
getline(fin,s);
fin.close();
cout<<s<<endl;
fout.open("Data.dat",ios::binary|ios::in);
fout.write((char *)&s,sizeof(s));
fout.close();
string temp;
fin.open("Data.dat",ios::binary);
fin.read((char *)&temp,sizeof(s));
cout<<temp;
return 0;
}
调试时访问内存访问冲突。