ndk ツールチェーンを使用して次のコードを作成し、Android のファイル操作の容量をテストしています。そして、/data では、読み取りまたは書き込みのパーミッションは間違いなく OK です。しかし、なぜ fopen() が機能せず、NULL を返すのか、私は混乱しています。コードは次のとおりです。
#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);
}
}
Android でコードを直接実行すると、次のプロンプトが表示されます。
# ./test
fopen error
pwrite error
何か案は?