使用C++复制一个文本文件内容

来源:互联网 发布:js设置属性值 编辑:程序博客网 时间:2024/04/28 23:09

#include<iostream>
using namespace std;
#include<fstream>
int main()
{
//const int IFileNameLen=40;
//const int OFileNameLen=40;
char s[40];
char s1[40];
cout<<"请输入要复制的源文件名字"<<endl;
cin>>s;//这里注意,用户输入文件名的格式为如下格式:D:/用户目录/Desktop/你的文件名.txt

//     注明:在XP以上的windows系统下文件名的输入格式可以是这两种方式:

//D:/用户目录/Desktop/你的文件名.txt 或者使用

//D:\\用户目录\\Desktop\\你的文件名.txt

//即:文件夹分隔符使用\\或者/

ifstream input;
ofstream output;
input.open(s);
if(input.fail())
{
cout<<"源文件打开失败"<<endl;

}
else
{
cout<<"请输入复制后的文件名"<<endl;
cin>>s1;
output.open(s1);
if(output.fail())
{
cout<<"打开复制后的文件失败"<<endl;
}
else
{
while(!input.eof())
{

output.put(input.get());

//先使用input.get()方法读取一个【字符】,然后将这个字符作为output.put()的参数,输出出来.


}

}
}
input.close();
output.close();
cout<<"复制成功"<<endl;
return 0;
}
0 0