2012-03-21 37 views
0

我想在我的Native C程序中將文件從目錄複製到另一個目錄。 我試過使用system函數,但它不工作。如何使用Android中的本機代碼將文件從一個目錄複製到另一個目錄?

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

此外,我想知道,即使system功能仿生libc中的支持。

任何幫助,將不勝感激。

+0

Android是Java,那麼Native C是什麼意思? – 2012-03-21 11:47:43

+0

@WaynnLue我在這裏使用Android NDK – user1089679 2012-03-21 11:48:50

+0

你有沒有考慮在java端做這件事? – enobayram 2012-03-21 11:48:51

回答

1

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

而且在AndroidManifest.xml中文件中提及

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

..

編輯:

呦你也可以使用dd if=source_file of=dest_file

不需要重定向支持。

+0

另外我想知道甚至系統函數在仿生libc中都受支持。 – user1089679 2012-03-21 12:16:55

+0

我也爲cp命令安裝了忙框。 cp命令在Android外殼上工作,但我想從本地C代碼調用此命令 – user1089679 2012-03-21 12:21:22

+0

我使用C++代碼中的** execl()**命令來執行adb命令。 – user370305 2012-03-21 12:38:25

相關問題