Is there any ordinary reason to use open() instead of fopen()?

前端 未结 7 1340
南旧
南旧 2021-01-05 13:05

I\'m doing a small project in C after quite a long time away from it. These happen to include some file handling. I noticed in various documentation that there are functions

7条回答
  •  醉话见心
    2021-01-05 13:52

    It is better to use open() if you are sticking to unix-like systems and you might like to:

    • Have more fine-grained control over unix permission bits on file creation.
    • Use the lower-level functions such as read/write/mmap as opposed to the C buffered stream I/O functions.
    • Use file descriptor (fd) based IO scheduling (poll, select, etc.) You can of course obtain an fd from a FILE * using fileno(), but care must be taken not to mix FILE * based stream functions with fd based functions.
    • Open any special device (not a regular file)

    It is better to use fopen/fread/fwrite for maximum portability, as these are standard C functions, the functions I've mentioned above aren't.

提交回复
热议问题