C++,用户从键盘输入若干行字符串,每输入一行就将其写入一个文本中
1个回答
展开全部
//#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();
}
#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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询