First, what is a document
Before we talk about file operations, we first need to know what files are. You may feel ridiculous to see this problem, because the file is the simplest concept for someone who has used a computer, for example, a text is a file, a work document is a file, and so on. But in Linux, the concept of a file is much more than that, and in Linux everything (or almost everything) is a file. Files include a lot of content, such as: You know the normal file is a file, directory is a file, the device is also a file, pipeline is a file and so on. The operation of the directory, the device, and so on, can be exactly equivalent to the operation of the plain text file, which is one of the most successful features of Linux.
Second, system call
1. File descriptor
File descriptors are small numbers that you can access through the open file device, and how many file descriptors are available depending on the configuration of the system. But when a program starts running, it typically has 3 file descriptors that are already open, which is
0: Standard input
1: Standard output
2: Standard error
Those math (i.e. 0, 1, 2) are file descriptors, because everything on Linux is a file, so standard input (stdin), standard output (stdout), and standard error (STDERR) can also be treated as files.
2, the system calls commonly used functions
A, open system call
The prototype of the Open function is:
int open (const char *path, int oflags);
int open (const char *path, int oflags, mode_t mode);
Path, is the full file name that includes the path, Oflags is the file access mode (that is, how to open the file, read-only, write-only, read-write, etc.), mode is used to set the access rights of the file. Specific optional parameters, you can view the manual page, here is not detailed.
Open creates an access path to a file or device, if the call succeeds, returns a file descriptor that can be used by functions called by Read, write, and so on, and this file description is unique, is not shared with any other running processes, and returns 1 on failure. and set the global variable errno to indicate the cause of blindness.
B, write system call
The prototype of the Write function is:
size_t write (int fildes, const void *buf, size_t nbytes);
Write writes the first nbytes bytes of the buffer buf to the file descriptor Fildes The associated file, returning the actual number of bytes written. A return of 0 indicates that no data has been written, and 1 indicates that an error occurred in the call and that the error code is stored in errno.
Note: Fildes must be the created file descriptor returned in the open call, or the standard input, output, or standard error of 0, 1, 2, and so on.
C, read system call
The prototype of the Read function is:
size_t read (int fildes, void *buf, size_t nbytes);
The read system call reads Nbytes bytes of data from a file descriptor-related file and places them in the data area buf, returns the number of bytes read, and returns 1 when it fails.
D, close system call
The function prototype for the close call is:
int close (int fildes);
The function of the close function is finally the association between the file descriptor fildes a corresponding file.
E, examples
Having said so much, I will give a complete example, from a data file (inside there are 1M ' 0 ' characters) copied to another file. The file name is COPY_SYSTEM.C and the code is as follows:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h >
int Main ()
{
char c = ' I ';
int in =-1, out =-1;
Open the data file in the read-only mode in
= Open ("Data.txt", o_rdonly);
Create a file as write-only, if the file does not exist create a new file
//File The owner has read and write permission out
= open ("Copy_system.out.txt", o_wronly| O_creat, s_irusr| S_IWUSR);
While reading (in, &c, 1) = = 1)/read a byte of data write
(out, &c, 1);//write a byte of data
//Close file descriptor closed
(in);
Close (out);
return 0;
}