简体   繁体   中英

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 .

I donot know the name of the file so i have put *.txt as source file.

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.

You could use the system() function to do this. For example, for Windows, you could just use the copy command to copy txt files.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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