将一个文本文件中的内容复制到另一个文本文件中,再将另一个文件中的内容读出显示到屏幕。怎样编写C语言

将source.txt文本文件中的内容复制到dest.txt文本文件中,
再将dest.txt文件中的内容读出显示到屏幕。

程序代码:
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
char ch;
ifstream file("C:/test.txt");//读取c盘的文本文件
ofstream file1("C:/test1.txt");//创建文本文件
while(file.get(ch))//读取文本中的内容
{
cout << ch;//输出文本内容到控制台
file1<<ch;//写入内容到文件
}
file.close(); //关闭文件流
file1.close();
cout<<endl;
}
注意这个程序会清空text1.txt中的内容然后在写入
以下的程序代码不会
程序代码:
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
char ch;
ifstream file("C:/test.txt");//读取c盘的文本文件
ofstream file1("C:/test1.txt",ios::app);//创建文本文件
while(file.get(ch))//读取文本中的内容
{
cout << ch;
file1<<ch;//写入内容到文件
}
file.close(); //关闭文件流
file1.close();
cout<<endl;
}
温馨提示:答案为网友推荐,仅供参考
相似回答