Get the App
SLTechnology News&Howtos  ›  Servers  › 

Learning notebook-Linux file IO

Shulou Source: shulou.com Published: 2022-06-03 09:49:35 11月16日 Update

Everything in Linux is a file!

This piece of content is mainly a variety of operations on Linux files: open, close, new, read, write, etc., C functions (not Shell commands!) As follows:

int open(const char *path,int oflags,mode_t mode)

[path: path/oflags: open mode (see Note 2)/mode: permission/return: handle, error: -1]

Note 1: If the file does not exist, create it.

Note 2: oflags are:

O_RDONLY: file read-only;(similar to "open as read-only" in excel to protect the file from being altered)

O_WRONLY: file write only;

O_RDWR: file readable and writable;

O_NOCTTY: If the path points to a terminal, the device is not used as the control terminal for this process (it can still be used if the path is a terminal console);

O_NDELAY: Non-blocking mode operation file

Note 3: A handle is actually a pointer to a file structure, like holding up a card in a crowd to say "I'm here, something's coming for me"

int close(int fd)

fd: Handle of an open file

int creat(const char *pathname,mode_t mode)

[path: path/mode: permission]

ssize_t read(int fd,void *buf,size_t len)

[fd: Handle of opened file/buf: Storage location of read data/len: Length of read data (bytes)/return: Actual number of bytes of read data, Error: -1]

ssize_t write(int fd,void *buf,size_t count)

[fd: Handle of opened file/buf: Data to be written/count: Length of data to be written (bytes)/return: Actual number of bytes of data written, Error: -1]

4 header files for file operations:

#include

#include

#include

#include

Where are the above header files at ×××? Type "man 2 open" under shell.

P.S. In order to better understand this chapter, it is recommended to review the relevant contents of Tan Haoqiang's C Language Programming (Fourth Edition), which has some conceptual things, fopen (), fclose (), etc.

Tags: File data handle path method terminal error head content byte actual permission length control thing crowd location function command that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Shulou Information Linux macOS