Linux programming-memoir 2: A memoir of linux Programming
=== File IO ====
Linux I/O operations use file descriptors to represent open files, a non-negative
An integer used to indicate all types of open files, including pipelines, FIFO, and network intercommunication.
Characters, terminals, devices, and general files. Each process has its own set of file descriptors.
The three commonly used file descriptors are:
Standard input (stdin, STDIN_FILENO, 0)
Standard output (stdout, STDOUT_FILENO, 1)
Standard Error (stderr, STDERR_FILENO, 2)
# Include <sys/stat. h>
# Include <fcntl. h>
Int open (const char * pathname, int flags,.../* mode_t mode */);
Open a file, return the file descriptor, fail to return-1, and set the errno Value
Pathname indicates the file path, flags indicates the access permission, and mode indicates the permission to set the file.
Flags values are usually combined as follows:
O_RDONLY read-only
O_WRONLY write-only open
O_RDWR read/write Enabled
...
# Include <unistd. h>
Ssize_t read (int fd, void * buffer, size_t count );
The maximum number of bytes read from the file to the buffer. The number of bytes read is returned successfully. 0 indicates that the file is read.
-1 indicates an error occurred.
# Include <unistd. h>
Ssize_t write (int fd, void * buffer, size_t count );
Extracts count bytes from the buffer, writes them to the file corresponding to fd, and returns the actually written
The number of bytes. The value-1 indicates an error.
# Include <unistd. h>
Int close (int fd );
Close the file. 0 succeeded,-1 failed.
# Include <unistd. h>
Off_t lseek (int fd, off_t offset, int whence );
Changes the offset of the file. The offset is the offset relative to the whence. A new offset is returned successfully. The error "-1" is returned.
Offset: the location where the file is read/written.
Whence value:
SEEK_SET (beginning with the file)
SEEK_CUR (current offset)
SEEK_END (end of the file)
# Include <sys/ioctl. h>
Int ioctl (int fd, int request,.../* argp */);
IO universal operation interface, a wide variety of operation types. For details, refer to the help manual. The return value depends on the request and-1 error.
# Include <fcntl. h>
Int fcntl (int fd, int cmd ,...);
Operation file properties, error returned-1, can be obtained or set various properties of the file
# Include <unistd. h>
Int dup (int oldfd );
Copy the file descriptor and return an unused minimum file descriptor. The error returns-1.
# Include <unistd. h>
Int dup2 (int oldfd, int newfd );
Copy the oldfd file descriptor as the newfd file descriptor. If newfd has been
Disabled first.
# Include <stdlib. h>
Int mkstemp (char * template );
Create a temporary file using the file template path. The last six bytes of the path must be XXXXXX.
The atomic row is guaranteed. The error returns-1.