C++ 问题,从键盘输入字符串,输入'end'时结束并保存到data.txt文件中,然后结束

C++ 问题,从键盘输入字符串,输入'end'时结束并保存到data.txt文件中,然后结束
磁盘本身没有data.txt这个文件。
望高手解答。谢了
分行输入也可以的那种。

Kwangwoon Univ.
Computer engineering
end.
这样输入之后就有
Kwangwoon Univ.
Computer engineering
保存在data.txt中

这是我自己编写的程序,在Code Blocks 里编译通过,可以实现你说的功能。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ofstream outfile;
outfile.open("text.txt",ofstream::out);
if(!outfile){
cerr<<"error:unable to open output file:"<<endl;
return -1;
}
string temp;
cin>>temp;
while(temp!="end"){
outfile<<temp<<" ";
cin>>temp;
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-28
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string s;
ofstream of;
of.open("data.txt");
cin>>s;
while(s!="end")
{
of<<s<<(char)getchar();
cin>>s;
}
of.close();
return 0;
}本回答被提问者采纳
相似回答