English 中文(简体)
和机器人文件操作, 打开, 失败
原标题:android file operation,open, fails

I am using the ndk toolchain to build the following code to test the capacity of android s file operations. And, in /data, the permission of read or write is no doubt Ok. But, I am confused as to why fopen() does not work and returns NULL. Here is the code:

#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main()
{
    int fd;
    int count = 128;
    int offset = 32;
    int ret;
    char buf[1024]="hi ! this is pwrite.";
    char pathname[128] = "/data/pwrite.txt";
    /*fd = fopen(pathname, O_WRONLY);*/
    FILE *infile;
    infile = fopen(pathname, "rb");
    if(infile==NULL) printf("fopen error  
");
    /*if(fd==-1)printf("open error 
");*/
    if((ret = pwrite(fd, buf, count, offset))==-1)
    {
        printf("pwrite error
");
        exit(1);
    }
    else
    {
        printf("pwrite success
");
        printf("the writed data is:%s", buf);
    }
}

当我在Android直接运行代码时,它会引发以下因素:

# ./test
fopen error  
pwrite error

有什么想法吗?

最佳回答

您在打开文件时给出了 < code>> rb 模式, 这仅意味着读取二进制文件。 现在您正在执行错误的 < code> write 操作。 您应该给出以下的写入模式,

infile = fopen(pathname, "wb");
问题回答

除了您的文件模式问题之外

"/data/pwrite.txt"

不应成为用于使用机器人应用程序的用户,甚至(在有保障的设备上)广告贝壳用户书面使用的可进入地点。

至于大约的机器人2.2上方(但随时会变化), /数据/ 本地可被adb shell 用户用作抓抓区。 对于应用程序中的代码, 每个应用程序的用户都拥有自己的软件包( 唯一) 私人存储区, 可以由 Java 级的 api 调用 之一找到 - 我认为它有点像 FilesDir () - 您真的应该用它来移动, 而不是硬编码路径 。

只有在您想要使用“ 外部存储 ” ( 取决于该存储器可能被存储的设备或实际可移动卡片) 而不是内部存储时, 才能使用 Wriit_ Exterrite_ STORAGAG 权限。 再次, 您应该通过 spi 调用来获取设备特定路径, 而不是不可移动地假设它是 / mt/ sdcard 之类的东西 。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签