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
有什么想法吗?