C++编写代码实现把文件A中的内容复制给文件B,并运行在屏幕显示出来
2个回答
展开全部
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream inFile;
string inFilename;
cout << "输入要复制的文本文件名:";
cin >> inFilename;
inFile.open(inFilename);
while (!inFile.is_open())
{
cout << "错误:找不到文件,请确认文件在程序所在目录中并被命名为" << inFilename << endl
<< "重新键入文件名:";
cin >> inFilename;
inFile.open(inFilename);
}
cout << "文件1读取成功!\n";
ofstream outFile;
string outFilename;
cout << "输入要复制到的文本文件名:";
cin >> outFilename;
outFile.open(outFilename);
while (!inFile.is_open())
{
cout << "错误:找不到文件,请确认文件在程序所在目录中并被命名为" << outFilename << endl
<< "重新键入文件名:";
cin >> outFilename;
inFile.open(outFilename);
}
cout << "文件2读取成功!\n";
cout << "单击回车开始复制......";
cin.get();
cin.get();
cout << "文件 " << inFilename << "中的内容:\n";
char ch;
while (inFile.good())
{
ch = '\n';
inFile.get(ch);
cout << ch;
outFile << ch;
}
inFile.close();
outFile.close();
cout << "已被复制到文件 " << outFilename << " 中\n"
"单击回车结束程序......";
cin.get();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询