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

2024-12-03 00:36:35
推荐回答(1个)
回答1:

//#include "stdafx.h"//vc++6.0加上这一行.
#include
#include
#include
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();
}