C++文件输入输出问题,如何向文件输入一行string类的字符
下面的错误怎么处理?类型是对的啊string类#include<iostream>#include<string>#include<fstream>usingnamesp...
下面的错误怎么处理? 类型是对的啊 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;} 展开
#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;} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询