我需要将文件从某个目录复制到另一个目录中,我在将strtok分配到数组的部分中,我觉得很困惑。我有2562个要复制的文件。我想我需要2D数组,但我总是会遇到错误。帮助...
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/types.h>
#include<windows.h>
#include<string.h>
char** str_split(char* a_str, const char a_delim);
DIR *dir;
struct dirent *sd;
FILE *source, *target;
int main(){
char *token;
int ctr;
char pathsource[40];
char pathtarget[40];
strcpy(pathsource,"C:\\");
strcpy(pathtarget,"C:\\");
system("pause");
dir = opendir(pathsource);
if(dir){
while( (sd=readdir(dir)) != NULL ) {
token = strtok(sd->d_name,"\n");
printf("%s\n",token);
}
closedir(dir);
}
return 0;
}
顺便说一下,我刚从C:\ _中删除了一点 - 这不是实际的代码。
答案 0 :(得分:1)
如果您使用的是Windows操作系统,则可以使用system("copy dir1\\*.txt dir2\\");
这样的命令,可以根据需要构建参数(命令字符串)。
例如:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main(void)
{
char comstart[] = "copy";
char pathsource[] = "D:\\testdir\\";
char copypattern[] = "*.*";
char pathtarget[] = "D:\\testdir2\\";
char * command;
// building command
command = (char*) malloc(strlen(comstart) + strlen(pathsource) + strlen(copypattern) + strlen(pathtarget) + 3);
if(!command)
{
printf("Unexpected error!\n");
return 1;
}
strcpy(command, comstart);
strcat(command, " ");
strcat(command, pathsource);
strcat(command, copypattern);
strcat(command, " ");
strcat(command, pathtarget);
// command execution
int res = 0;
res = system(command);
if( res == 0)
{
printf("Files copied successfully!\n");
}
else
{
printf("Unexpected error with code %d!\n", res);
}
return 0;
}
修改强>
或者您可以使用Win API函数的更高级方法。参见:
和其他