简体   繁体   English

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

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

I want to copy file from on directory to another in my Native C program. 我想将文件从目录复制到本机C程序中的另一个目录中。 I tried using system function but it's not working. 我尝试使用system功能,但无法正常工作。

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

Also I want to know that even system function is supported in bionic libc. 我也想知道,仿生libc甚至支持system功能。

Any help would be appreciated. 任何帮助,将不胜感激。

The Android shell does not have the cp command. Android Shell没有cp命令。 So if possible try cat source_file > dest_file . 因此,如果可能,请尝试使用cat source_file > dest_file

Or just use this code, 或者只是使用此代码,

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

Also mentioned 还提到

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

in AndroidManifest.xml file.. AndroidManifest.xml文件中。

EDIT: 编辑:

You can use also dd if=source_file of=dest_file . 您也可以使用dd if=source_file of=dest_file

Redirection support is not needed. 不需要重定向支持。

暂无
暂无

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

相关问题 如何使用adb将文件复制到可从PC访问的android目录 - How to copy file using adb to android directory accessible from PC 如何将文件从根目录复制到无根android手机的另一个目录中? - How can i copy a file from a rooted directory to another directory in a non rooted android phone prorammatically? 如何将文件从一个目录复制到另一目录? - How to copy my files from one directory to another directory? 如何在 Android Studio 中将包从一个目录导入到另一个目录? - How to import Package from One Directory to another Directory in Android Studio? 将.3gp 文件从一个目录“External Dir”移动或复制到 Android 中的另一个外部目录 - Move or copy .3gp files from one directory “External Dir” to another External directory in Android Android路径已更改:错误将文件从一个文件夹复制到另一个文件夹 - Android Path is changed: Error Copy file from one folder to another 如何在不丢失 VCS 历史记录的情况下将代码从一个文件复制/粘贴到另一个文件 - How to copy/paste code from one file to another without loosing VCS history Android Studio:如何将.sfb文件从一个项目复制/粘贴到另一个项目? - Android Studio : how to copy/paste a .sfb file from one project to another? 如何使用 Appium 从一个原生应用程序切换到另一个原生应用程序? - How to switch from one native to another native app using Appium? 如何在代码中从一个LinearLayout复制参数并将其设置为另一个LinearLayout? - How to copy params from one LinearLayout and set to another LinearLayout in code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM