简体   繁体   English

将文件从SD卡位置复制到Android中C语言的另一个位置

[英]Copy a file from sdcard location to another location in c language in Android

I have written a code to copy a text file from one location( /mnt/sdcard/Appfolder/filename.txt ) to another( /data/test/log.txt ) in Android device in C language and building it using ndk . 我已经写了代码从一个位置(复制的文本文件/mnt/sdcard/Appfolder/filename.txt )到另一个( /data/test/log.txt在Android设备中的C语言),并用它构建ndk

I donot know the name of the file so i have put *.txt as source file. 我不知道文件名,所以我把* .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;
}

But I am getting this error: 但我收到此错误:

cannot open /mnt/sdcard/Appfolder/<filename.txt>

What am I doing wrong? 我究竟做错了什么?

The file attributes of the is 660. 的文件属性为660。

You could use the system() function to do this. 您可以使用system()函数执行此操作。 For example, for Windows, you could just use the copy command to copy txt files. 例如,对于Windows,您可以仅使用copy命令复制txt文件。

system("copy C:\src\dir\*.txt C:\dest\dir\");

Or with variables (pseudo code): 或带有变量(伪代码):

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM