The article directories

    • preface
    • Files under Linux
      • File descriptor
    • File I/O operations
      • Open/create the file
      • Close the file
      • Read/write files
      • File pointer offset

preface

Unconsciously, to the junior. Before you know it, you’re looking for a summer internship. Look at the old and you’ll know the new. (After reviewing the data structure for two days, I still prefer this one.) So here we are.

Okay, back to the point. Since I wrote server-side programming when I first came to CSDN, I will delete it whenever I refurbish it, so this update is another destruction of the old “antique” blog. But, neither break nor break!!


Files under Linux

To quote a classic phrase, “Everything is a file under UNIX.” A file is an abstraction mechanism that provides a way to store information and read it later.

After you create a file, it gives it a name. When a process terminates, the file continues to exist and can be accessed by other processes using the name.

File descriptor

File descriptors are used to represent normal files and device files under Linux. A file descriptor is an integer, and all operations on a file are done through the file descriptor. File descriptors are hubs in the file system that connect the user to the kernel space. When a file is opened and created, the kernel space creates the corresponding structure and generates an integer variable that is passed to the corresponding process in user space. The process uses this file descriptor to operate on files.

The file descriptor ranges from 0 to OPEN_MAX and is therefore a limited resource that should be released as soon as it is used up. File descriptors are valid only for a single process, i.e. file descriptors for different processes. The same value may describe different files.

There are three allocated file descriptors in Linux:

0STDIN_FILENO Standard input stream1STDOUT_FILENO Standard output stream2STDERR_FILENO Standard error streamCopy the code

These three file descriptors and their respective functions are tied together.


File I/O operations

Open/create the file

Under Linux, the open function can be used to open or create a file:

#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);

// We usually use fd to receive the return value
// Return value: Returns the reassigned file descriptor on success, -1 and errno on error
Copy the code

The first file name, the second open mode, and the third, if you want to write, file permission.

Open mode:

O_RDONLY Read-only O_WRONLY Write only O_RDWR read and write O_APPEND append O_CREAT if yes, create if no. O_EXCL checks to see if the file exists. If O_CREAT is also specified, an error is reported if the file exists. O_TRUNC Clears the file if it exists. O_NONBLOCK Opens a file in non-blocking mode. By default, it is blocked, meaning that reading or writing to a file must wait for the operation status to returnCopy the code

The last mode. This can only be used if O_CREAT exists.

The create function can be understood as a special case of the open function:

int open(pathname,O_WRONLY|O_CREAT|O_TRUNC,mode);
Copy the code

Create with O_EXCL improves the fault tolerance of programs.


Close the file

#include <unistd.h>

int close(int fd); //fd: file descriptor
// Return values: 0 on success, -1 on failure and set errno
Copy the code

Read/write files

The read function reads data from an open file. The write function writes data to an open file

#include <unistd.h>

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

// Return value: The number of bytes read/written on success, -1 on failure and errno.

/* buf: cache, usually with char array count: number of bytes to read/write */
Copy the code

Ssize_t is a symbol number that may be different from int or long. It may be implemented as int or long.


File pointer offset

The lseek() function sets the position of the file offset.

#include <sys/types.h>
#include <unistd.h>

off_t lseek(int fd,off_t offset,int whence);
// The offset is allowed beyond the end of the file.

/* offset: indicates the offset of the file, which can be negative whence: indicates the operation mode */
Copy the code

Operation mode:

If: SEEK_SET, offset is the position at the beginning of the relative file if: SEEK_CUR, offset is the position relative to the current position If: SEEK_END, offset is the position relative to the end of the fileCopy the code

The function returns the offset of the file on success, so you can offset 0 positions in SEEK_CUR mode to get the current offset