我编写了一段代码,将文本文件从一个位置 (/mnt/sdcard/Appfolder/filename.txt
) 复制到另一个位置 (/data/test/log.txt
) 在 Android 设备中使用 C 语言并使用 ndk
构建它。
我不知道文件的名称,所以我把 *.txt 作为源文件。
int copy_file(char* src, char *dest) {
FILE *p,*q;
char *file1,*file2;
int ch;
file1 = src;
p=fopen(file1,"r");
if(p==NULL){
printf("cannot open %s",file1);
exit(0);
}
file2 = dest;
q=fopen(file2,"w");
if(q==NULL){
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf("\nCOMPLETED");
fclose(p);
fclose(q);
return 0;
}
但是我收到了这个错误:
cannot open /mnt/sdcard/Appfolder/<filename.txt>
我做错了什么?
是660的文件属性。
最佳答案
您可以使用 system()
函数来执行此操作。例如,对于 Windows,您可以只使用复制命令来复制 txt 文件。
system("copy C:\src\dir\*.txt C:\dest\dir\");
或者用变量(伪代码):
#define PATH_MAX 4096
char command[MAX_PATH * 2 + 6];
char *file1 = src, *file2 = dest;
strcpy(command, "copy ");
strcat(command, file1);
strcat(command, " ");
strcat(command, file2);
system(command);
关于android - Android中c语言将文件从sdcard位置复制到另一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072859/