首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

拷贝索引

2013-08-04 
拷贝目录#include fstream#include iostream#includeio.h#includestring#includecstring#includ

拷贝目录
#include <fstream>
#include <iostream>
#include<io.h>
#include<string>
#include<cstring>
#include<direct.h>

using namespace std;

/*
* 路径转换,将单斜杠转换成双斜杠
*/
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                       //如果找到下个文件名字成功的话
{
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);                             //构建目录的源路径 


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;
}

然后它会报错c:\users\wangbuer\documents\visual studio 2008\projects\mycopydir\mycopydir\mycopydir.cpp(134) : error C2664: “std::basic_istream<_Elem,_Traits>::read”: 不能将参数 1 从“unsigned char [100]”转换为“char *”

1>c:\users\wangbuer\documents\visual studio 2008\projects\mycopydir\mycopydir\mycopydir.cpp(135) : error C2664: “std::basic_ostream<_Elem,_Traits>::write”: 不能将参数 1 从“unsigned char [100]”转换为“const char *”



还有有人有文件这部分的知识的链接也麻烦给我留下~~~

[解决办法]
unsigned char buf[100];
改为:
char buf[100];
[解决办法]
cin 输入字符串的时候,不需要用双斜杠来转义
只有 char fname[] = "c:\\temp\\aa.txt" 这样的字符串常量赋值的时候才需要转义。
[解决办法]
Windows的路径最长是260,所以数组大小最好也是这个。
即MAX_PATH的值。
[解决办法]
copy
or
cp -r

热点排行