繁体   English   中英

如何使用Android中的本机代码将文件从一个目录复制到另一个目录?

[英]How to copy file from one directory to another using native code in Android?

我想将文件从目录复制到本机C程序中的另一个目录中。 我尝试使用system功能,但无法正常工作。

system("cp /mnt/test /mnt/test2"); // It's not working

我也想知道,仿生libc甚至支持system功能。

任何帮助,将不胜感激。

Android Shell没有cp命令。 因此,如果可能,请尝试使用cat source_file > dest_file

或者只是使用此代码,

FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen("Source File", "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen("Destination File", "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

还提到

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

AndroidManifest.xml文件中。

编辑:

您也可以使用dd if=source_file of=dest_file

不需要重定向支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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