文件复制

#include<fstream.h>
#include<iostream.h>
#include<io.h>
#include<string>
#include<cstring>
#include<direct.h>




/*
* 路径转换,将单斜杠转换成双斜杠
*/
void getDouble(char * str, int len, char * temp) //
{
char * start = NULL;
char * t = NULL;
start = str;
t = temp;
for(int i = 1; i <= len; i++, ++start) //循环len次,来处理'\'
{
if(* start == '\\') //当为'\'时,在后面再加入一个'\'
{
* t = * start;
++t;
* t = '\\';
++t;
}else{ //不为'\'时,原样复制到新空间
* t = * start;
++t;
}
}
* t = '\0';
//cout<<temp<<endl; //输出路径
}


/*
* 当有通配符*时,得到父路径
*/
void getParentPath(char * str, int len, char * temp) //得到父路径
{
char * start = NULL;
char * end = NULL;
char * t = NULL;
start = str;
end = str + (len - 1); //指向最后一个位置
t = temp;
while( * end != '\\'){  //将end指向'\'
end = end - 1;
}
++end; //将指针放到'\'后面


for(; start != end; start++) //将父路径写到temp中
{
* t = * start;
++t;
}
* t = '\0'; //加'\0'结束
//cout<<"Parent Path:"<<temp<<endl;
}
/*
* 得到目的路径
*/
void getDesPath(char * des, char * desPath) //得到目的路径
{
strcpy(desPath, (const char *)des);
strcat(desPath, "\\\\");
//cout<<"Des Path:"<<desPath<<endl;
}


void getCommend(char * p, char * src, char * des)
{
strcpy(p, "xcopy ");
strcat(p, (const char *)src);
strcat(p, " ");
strcat(p, (const char *)des);
strcat(p, " /s/e");


//cout<<"命令:"<<p<<endl;

}


void fileCopy(char * src, char * des){
long lf; //定义打开文件的句柄
_finddata_t file; //结构体,存储文件的信息
char currentPath[100];
char transSrcPath[100];
char transDesPath[100];
char desPath[100];
unsigned char buf[100];



if((lf = _findfirst((const char *)src, &file)) != -1L) //对c盘a文件夹进行复制
{
//cout<<"文件列表:"<<endl;
do  //如果找到下个文件名字成功的话
{
/*
cout<<file.name<<endl;
if(file.attrib == _A_NORMAL)
cout<<"普通文件"<<endl;
else if(file.attrib == _A_RDONLY)
cout<<"只读文件"<<endl;
else if(file.attrib == _A_HIDDEN)
cout<<"隐藏文件"<<endl;
else if(file.attrib == _A_SYSTEM)
cout<<"系统文件"<<endl;
else if(file.attrib == _A_SUBDIR)
cout<<"子目录"<<endl;
else cout<<"存档文件"<<endl;
*/
getDouble(src, strlen((const char *)src), transSrcPath); //将转换的源路径存入transPath
getParentPath(transSrcPath, strlen(transSrcPath), currentPath); //得到父路径 c:\\a\\

getDouble(des, strlen((const char *)des), transDesPath); //将转换的目的路径存入transDesPath
getDesPath(transDesPath, desPath); //得到目的路径 c:\\b\\



if(file.attrib == _A_SUBDIR){ //如果为子目录
/*
* 当为子目录的时候,利用系统的命令行参数
* 实现子目录以及子目录内文件的拷贝
*/


char dirPath[100];
char cmd[100];
getParentPath(src, strlen((const char *)src), dirPath);
strcat(dirPath, file.name); //构建目录的源路径 
//cout<<"目录路径:"<<dirPath<<endl; // c:\a\bbbabc
getCommend(cmd, dirPath, des);
system((const char *)cmd); //调用系统的命令行参数实现文件夹的拷贝


}else{ //如果不是目录
/*
* 当文件不是目录时,利用fstream文件输入输出流来对每个文件
* 进行读/写操作,从而,达到复制的效果
*/


ifstream fin((const char *)strcat(currentPath, file.name), ios::nocreate|ios::binary); //创建输入文件流
ofstream fout((const char *)strcat(desPath, file.name), ios::binary); //创建输入流

if(!fin){
cout<<"源文件路径没有找到!"<<endl;
return;
}
if(!fout){
cout<<"目的路径错误!"<<endl;
return;
}
while(!fin.eof()){ //实现文件的复制
fin.read(buf, sizeof(buf));
fout.write(buf, fin.gcount());
}
fin.close(); //关闭流
fout.close();
}

}while(_findnext(lf, &file) == 0);
cout<<"复制已完成!"<<endl;
_findclose(lf);
}else{
cout<<"源文件路径没有找到!"<<endl;
}
}


int main()
{
char src[100], des[100]; 
cout<<"请输入路径和源文件名称:"<<endl;
cin>>src; // c:\\a\\*abc.txt
cout<<"请输入目的路径:"<<endl;
cin>>des;
fileCopy(src, des); //调用文件拷贝函数


return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值