.. include:: Files: ``open/read/write/close`` (Overview) =========================================== ``open()`` ---------- .. sidebar:: Documentation * `man -s 2 open `__ * `man -s 2 close `__ Swiss army knife: multiple actions, governed by bitwise-or'ed flags: * Create/Open/Truncate/... * Access intention ("mode"): open for reading, writing, both * Hundreds of others .. code-block:: c #include int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); ``open()`` Flags ---------------- .. sidebar:: See also * :doc:`/trainings/material/soup/linux/basics/permissions/basics` **Access Mode** .. sidebar:: Examples * :doc:`example-O_RDONLY` * :doc:`example-O_WRONLY` * ``O_RDONLY``: Can only ``read()``; error on ``write()`` * ``O_WRONLY``: Can only ``write()``; error on ``read()`` * ``O_RDWR``: ... **Creating a File** * ``O_CREAT``: create if not exists, else simply open * ``O_CREAT|O_EXCL``: *exclusive creation* * *error* if file exists * |longrightarrow| to prevent race conditions when two parties try to create a file at the smae time * security measure **Miscellaneous** * ``O_APPEND``: write access appends at the end * ``O_TRUNC``: truncate file to zero length if already exists * ``O_CLOEXEC``: ``exec()`` closes the file descriptor (|longrightarrow| later) ``read()`` ---------- .. sidebar:: Documentation * `man -s 2 read `__ .. code-block:: c #include ssize_t read(int fd, void *buf, size_t count); * Return value: number of bytes read (-1 on error, :doc:`as always `) * ``0`` is *End of File* (EOF) * Can read less than ``count`` (usually with network I/O) ``write()`` ----------- .. sidebar:: Documentation * `man -s 2 write `__ .. code-block:: c #include ssize_t write(int fd, const void *buf, size_t count); * Return value: number of bytes written (-1 on error, :doc:`as always `) * Can write less than ``count`` (usually with network I/O) * ``SIGPIPE`` (process termination) when ... * Writing to a network connection that has been closed by peer * Writing to a pipe where receiver end has closed/exited (hence ``SIGPIPE``)