Knowledge about file I/O operations under Linux

  

Linux file I/O mainly refers to file input and output. Many beginners don't know much about I/O of files, and Linux file I/O operations are more. The following small series will give you a detailed introduction to Linux file I /O.

linux file I /O tutorial (1)

First, the file descriptor

For the kernel, so open files referenced by file descriptor . Each process has some file descriptors associated with it. The file descriptor is a non-negative integer. When opening an existing file or creating a new one, the kernel returns a file descriptor to the process. When reading or writing a file, the file is identified by the file descriptor returned by open or creat, which is passed as a parameter to read and write.

There are generally three open file descriptors, they are:

The code is as follows:

0: Standard input STDIN_FILENO

1: Standard output STDOUT_FILENO

2 standard error output STDERR_FILENO

The symbol constants after each line are POSIX dependent.

open function

The code is as follows:

#include "sys/types.h"

#include "sys/stat.h"

#include "fcntl.h"

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

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

pathname is the name of the file to open or create.

The flag is used to define the action taken to open the file. One of the following modes must be called.

O_RDONLY, O_WRONLY, O_RDWR respectively represent read-only, write-only, read-write mode.

open can also include a combination of the following optional modes

O_APPEND: Append write data to the end of the file

O_CREAT: If the file does not exist, create it . When using this option, you need the third parameter mode, which is used to specify access rights for this new file.

O_EXCL: If O_CREAT is specified at the same time and the file exists, an error will occur. Use this to test if a file exists and, if it exists, create a file, which makes testing and creating both an atomic operation.

O_TRUNC: If this file exists and is successfully opened for write-only or read-write, its length is truncated to 0.

The file descriptor returned by open must be the smallest unused descriptor value. This is used by some applications on standard input, standard output or standard error output. For example, if a program closes its standard output and then calls open again, file descriptor 1 will be called and the standard output will be effectively redirected to another file or device.

The POSIX specification also standardizes a creat call that is equivalent to

open(pathname,O_WONLY | O_CREAT |  O_TRUNC, mode);

close function

#include "unistd.h"

int close(int fd);

close call terminates a file The association between the descriptor fd and the corresponding file. The file descriptor is released and can be reused. The close call returns 0 successfully, and the error returns -1.

When a file is closed, all record locks added to the file by the process are released. When a process terminates, the kernel automatically closes all its open files.

lseek function

Each open file has an associated file offset & rdquo; associated with it. By default, when a file is opened, the offset is set to 0 unless the O_APPEND option is specified. Lseek can set an offset for an open file.

The code is as follows:

#include "sys/types.h"

#include "unistd.h"

off_t lseek(int fd, off_t Offset, intwhence);

offset is used to specify the position, and the whatence parameter defines the usage of the offset value. Whence can take the following values:

The code is as follows:

SEEK_SET: The offset is set to offset bytes.

SEEK_CUR: The offset is set to its current locationplus offset bytes. Br>

SEEK_END: ​​The offset is set to the size of the fileplus offset bytes.

The successful call returns the byte offset value from the file header to the file pointer being set, failing to return -1. The parameter offset is defined in "sys/types.h".

When the offset is greater than the file length, a hole appears and the hole does not occupy the storage area.

read function

The code is as follows:

#include "unistd.h"

ssize_t read(int fd, void *buf, size_tcount); Br>

Put the count characters in the file associated with the file descriptor fd into the buf. Returns the number of bytes read in, which may be less than the number of bytes requested. If the read call returns 0, it means that no data has been read and the end of the file has been reached. Returning -1 indicates an error.

write function

The code is as follows:

#include "unistd.h"

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

Writes the first count bytes of the buffer buf to the file associated with the file descriptor fd. Returns the number of bytes actually written, usually the same as the count value; otherwise it indicates an error. A common cause of errors is that the disk is full or exceeds the file length limit for a given process.

Instance: Create a file, write data, move the current offset, and read the data.
Previous123Next page Total 3 pages

Copyright © Windows knowledge All Rights Reserved