大家好!我使用ndk工具鏈構建以下代碼來測試android文件操作的容量。 而且,在/ data中,讀或寫的權限無疑是OK。但是,我感到困惑,fopen()不起作用並返回NULL。 下面是代碼:android文件操作,打開,失敗
#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
任何想法THX
嘗試char pathname [128] =「//data//pwrite.txt」 ; – Lucifer
我有嘗試你的想法,但它也失敗了。但是,無論如何thx。任何其他的想法? –
okey,請試試這個'infile = fopen(* pathname,「rb」);' – Lucifer