我在写入字符串到二进制文件时遇到了问题。这是我的代码:
ofstream outfile("myfile.txt", ofstream::binary);
std::string text = "Text";
outfile.write((char*) &text, sizeof (string));
outfile.close();
然后,我试着去阅读它,
char* buffer = (char*) malloc(sizeof(string));
ifstream infile("myfile.txt", ifstream::binary);
infile.read(buffer, sizeof (prueba));
std::string* elem = (string*) buffer;
cout << *elem;
infile.close();
我就是无法让它工作。对不起,我只是绝望了。谢谢!
string
是std::string
,这段代码是无法编译通过的:string *text = "Text";
- jrok