C++文件输入输出问题,如何向文件输入一行string类的字符

下面的错误怎么处理? 类型是对的啊 string类
#include<iostream>#include<string>#include<fstream>using namespace std;
int main(){ string str,str1; str="hello"; cout<<sizeof(str)<<endl; str="Hello world!"; cout<<sizeof(str)<<endl; //string类只是一个指针,所以两个返回值相同都h是4
ofstream fout("abc.txt",ios::app); fout<<str<<endl; cout<<"input a line string:"<<endl; getline(cin,str1); fout.write((char*)&str1.c_str(),str.size()); return 0;}

string.c_str()本身就返回了一个char*了,你这串(char*)&是什么意思?
写入可以直接
fout<<str1;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-02
把 (char*)& 去掉本回答被网友采纳
相似回答