C++,用户从键盘输入若干行字符串,每输入一行就将其写入一个文本中

如题所述

//#include "stdafx.h"//vc++6.0加上这一行.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main(void){
string str;
fstream iofile("text.txt",ios::out);
if(!iofile){
cout << "Create the file failure...\n";
exit(0);
}
cout << "Enter some strings...\n";
while(1){
getline(cin,str);
if(str=="end") break;
iofile << str << endl;
}
iofile.close();
iofile.open("text.txt",ios::in);
while(!iofile.eof()){
iofile >> str;
cout << str << endl;
}
iofile.close();
}
温馨提示:答案为网友推荐,仅供参考
相似回答