[英]Copy a file from sdcard location to another location in c language in Android
我已經寫了代碼從一個位置(復制的文本文件/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,您可以僅使用copy命令復制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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.