open file fail(-1) with different flag settings

不打扰是莪最后的温柔 提交于 2019-12-25 03:14:29

问题


I have a question about using open() with different flags in Native android.

Because i want to open a file and ignore the cache& buffer, in oder to access the hardware(SD card) directly. If the flag setting is O_CREAT | O_RDWR | O_NDELAY, S_IRUSR | S_IWUSR| O_DIRECT | O_SYNC . I can got a positive file descriptor(fd).

But if I change the setting to O_CREAT | O_RDWR | S_IRUSR | S_IWUSR| O_DIRECT | O_SYNC the result is fail(-1).


回答1:


If the flag setting is O_CREAT | O_RDWR | O_NDELAY, S_IRUSR | S_IWUSR| O_DIRECT | O_SYNC . I can got a positive file descriptor(fd).

That is not quite correct usage of the form

int open(const char *pathname, int flags, mode_t mode);

But if I change the setting to O_CREAT | O_RDWR | S_IRUSR | S_IWUSR| O_DIRECT | O_SYNC the result is fail(-1).

What you call "setting" is an invalid mixture of flags and mode symbols. Also, since O_CREAT has been specified in flags, the mode argument must be supplied and it is not.
Try separating the modes from the flags:

open(pathname, O_CREAT | O_RDWR | O_DIRECT | O_SYNC, S_IRUSR | S_IWUSR);


来源:https://stackoverflow.com/questions/28165493/open-file-fail-1-with-different-flag-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!