Android fopen write file


everyone!i use the ndk toolchain to build the following codes to test the capacity of android’s file operations. And,in the /data,the permission of read or write is no-doubt Ok.But,i feel confused that the fopen() does not work and return NULL. Here is the code:

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 \n”);
/*if(fd==-1)printf(“open error \n”);*/
if((ret = pwrite(fd, buf, count, offset))==-1)
printf(“pwrite error\n”);
printf(“pwrite success\n”);
printf(“the writed data is:%s”, buf);

When i run the code directly in the android,it prompts following:

# ./test
fopen error
pwrite error

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>