# 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); 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;}
// 为什么write()和read()的第二操作数可以是字符数,不应该是字节数是吗?