# include<iostream>
# include<string>
# include<fstream>
using namespace std;
void f1()
{
ofstream out("尝试.dat", ios::binary | ios::out);
string str;
str = "ABC";
int len = str.size();
cout<<len<<endl;
out.write((char *)(& len), sizeof(int));
out.write(str.c_str(), len); // 为什么是len, 而不是len * sizeof(cahr)?
out.close();
}
void f2()
{
ifstream in("尝试.dat", ios::binary | ios::in);
int length;
char * p;
in.read((char *)(& length), sizeof(int));
p = new char[length + 1];
in.read(p, length);
p[length] = '\0';
cout<<p<<endl;
string st;
st = p;
cout<<st;
in.close();
}
int main()
{
f1();
f2();
return 0;
}
onemoo
慕的地0536417
慕的地0536417