[英]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.