簡體   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