Issue
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 \n");
/*if(fd==-1)printf("open error \n");*/
if((ret = pwrite(fd, buf, count, offset))==-1)
{
printf("pwrite error\n");
exit(1);
}
else
{
printf("pwrite success\n");
printf("the writed data is:%s", buf);
}
}
When I run the code directly in Android, it prompts the following:
# ./test
fopen error
pwrite error
Any ideas?
Solution
You have given rb
mode while opening the file, which simply means reading binary file. Now you are performing the write
operation which is wrong. You should give the write mode as follows,
infile = fopen(pathname, "wb");
Answered By - Lucifer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.