clothing是类,那么 Clothes 是什么?应当是结构。
Clothes temp;
fout.write((char*) &temp,sizeof(Clothes) ); //调用 write() 输出整个结构
fout.flush(); // 调用 flush()
// cout<
修改如下,我就不验证了,自己验证一下pstring类里面有一个函数c_str().可以把string类的对象转为char#include &lt;fstream&gt; #include&lt;iostream&gt; #include &lt;string&gt; using namespace std; int main(){ string str(&quot;abcdefg&quot;); ofstream outFile(&quot;atxt4.txt&quot;); if(!outFile){ cerr&lt;&lt; &quot;unable to open output file: &quot; &lt;&lt; &quot;atxt4.txt&quot; &lt;&lt; &quot; -- bailing out!&#92;n&quot;; return -1; } outFile.write(str.c_str()4);&#47;*要求一个字符串类型char*所以要转换*&#47; cout&lt;&lt; str; return 1; }