2012-05-28 37 views
-2

android是一种linux,它必须支持posix.But,当它似乎不支持syscall时,open()。 下面是测试代码,而我通过NDK编译:android似乎不支持系统调用,打开()

#include <unistd.h> 
#include <stdio.h> 
#include<sys/types.h> 
#include<sys/stat.h> 
#include<fcntl.h> 

void main(){ 
    int fd; 
    char pathname[128] = "/data/pwrite.txt"; 
    fd = open(pathname, O_WRONLY); 
    if(fd==-1){ 
     printf("open fail.\n"); 

    } 
    perror("/data/pwrite.txt"); 
} 

及以下是来自Android的提示:

[email protected]:~$ adb shell /data/pwrite/test1 
open fail. 
/data/pwrite.txt: No such file or directory 
+0

而不是仅仅打印“打开失败”,而是打印实际的错误。你可以通过打印'errno'或'strerror'返回的字符串,或者使用'perror'函数来做到这一点。 –

+0

kaiwii @ ubuntu:〜$ adb shell/data/pwrite/test1 打开失败。 /data/pwrite.txt:没有这样的文件或目录 –

+0

@Joachim Pileborg我使用perror打印错误,上面是它提示。但是,我感到困惑,为什么不open()创建文件.thx –

回答

1

我认为问题出在旗帜上 - 你只能用O_WRONLY。但是如果文件不存在,您还应该使用O_CREAT标志创建它。所以如果一个文件不存在,你应该打电话:

fd = open(pathname, O_WRONLY | O_CREAT); 
1

我觉得这个问题是不是syscall open()但事实证明你正试图访问/data。该文件夹仅适用于有根据的移动设备或模拟器。你有没有试图将文件放入/sdcard文件夹?

+0

但是,当我使用fopen()来完成相同的工作时,它可以工作。 –

+0

并且我在模拟器中运行以下代码 –