Use C language to manipulate the basic functions of the file collation

  • 2020-04-02 03:19:21
  • OfStack

C creat() function: creates a file function

The header file:


#include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>

Definition function:


int creat(const char * pathname, mode_tmode);

Function description:
1. The parameter pathname refers to the file path string to be created.
2. Creat() is equivalent to calling open() using the following method
      Open (const char * pathname, (O_CREAT|O_WRONLY|O_TRUNC));

Error code: refer to the open() function for the parameter mode.

The return value:

Creat () returns a new file descriptor, -1 if an error occurs, and sets the error code to errno. EEXIST parameter: the file referred to by pathname already exists. EACCESS parameter: the file specified by pathname does not meet the required test permissions EROFS: the file you want to open write exists in a read-only file system EFAULT parameter: the pathname pointer exceeds the accessible memory space EINVAL parameter: mode is not correct. ENAMETOOLONG parameter: pathname is too long. ENOTDIR parameter: pathname is a directory ENOMEM: out of core memory ELOOP parameter: pathname has too many symbolic joins. EMFILE: the maximum number of files a process can open at the same time has been reached ENFILE: the maximum number of files the system can open at the same time has been reached

Note: creat() cannot create a special device file, use mknod() if necessary.


C language open() function: open the file function

The header file:


#include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>

Definition function:


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

Function description:

The pathname parameter points to the file path string to be opened. The following are the flags that the parameter flags can use:

O_RDONLY opens the file read-only O_WRONLY opens the file in write-only mode O_RDWR opens the file in read-write mode. The three flags are mutually exclusive, meaning they cannot be used together, but can be combined with the following flags using the OR(|) operator. O_CREAT automatically creates the file you want to open if it does not exist. O_EXCL if O_CREAT is also set, this command checks to see if the file exists. If the file does not exist, the file is created, otherwise it will cause an error to open the file. O_NOCTTY will not treat the open file as a process control terminal if it is a terminal device. When a file exists and is opened in a writable manner, this flag clears the length of the file to 0 and the information originally stored in the file disappears. O_APPEND moves from the end of the file when a file is read or written, that is, the data written is appended to the end of the file. O_NONBLOCK opens the file in an uninterruptible manner, meaning that it is immediately returned to the process with or without data read or waiting. O_NONBLOCK O_NDELAY. O_SYNC opens files synchronously. O_NOFOLLOW fails to open a file if the file referred to by the pathname parameter is a symbolic connection. O_DIRECTORY if the file referred to by the pathname parameter is not a directory, it will fail to open the file. Note: this is a special flag after linux2. to avoid some system security problems.

The parameter mode has the following combinations, which will only take effect when a new file is created. In addition, the permissions when the file is actually created will be affected by the umask value, so the file permissions should be (mode-umaks).

S_IRWXU00700, which represents the file owner with readable, writable, and executable permissions. S_IRUSR or S_IREAD, 00400 authority, which represents that the file owner has readable authority. S_IWUSR or S_IWRITE, 00200 authority, which represents that the file owner has writable authority. S_IXUSR or S_IEXEC, 00100 authority, which represents that the file owner has executable authority. S_IRWXG 00070, which represents the file user group with readable, writable, and executable permissions. S_IRGRP 00040 privilege, which means that the file user group has readable permissions. S_IWGRP 00020, which represents the file user group with writable permissions. S_IXGRP 00010 permissions, which represent executable permissions for the file user group. S_IRWXO 00007, which represents other users with readable, writable, and executable permissions. S_IROTH 00004 privilege, which means that other users have readable permissions S_IWOTH 00002 permissions, which represent writable permissions for other users. S_IXOTH 00001 permissions, which represent other users with executable permissions.

Return value: if all the permissions to be verified pass the check, return a value of 0, indicating success. If one of the permissions is disabled, return -1.

Error code:

The file referred to by the EEXIST parameter pathname already exists, but the O_CREAT and O_EXCL flags are used. The file referred to by the EACCESS parameter pathname does not conform to the required test permissions. The file that EROFS wants to test for write permissions exists in a read-only file system. The EFAULT parameter pathname pointer exceeds the accessible memory space. EINVAL parameter mode is not correct. The ENAMETOOLONG parameter pathname is too long. The ENOTDIR parameter pathname is not a directory. ENOMEM is out of core memory. The ELOOP parameter pathname has too many symbolic joins. EIO I/O access error.

Additional note: use access() role user authentication judgment to be particularly careful, for example, after access() to make an open() empty file may cause system security problems.

sample


#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
  int fd, size;
  char s[] = "Linux Programmer!n", buffer[80];
  fd = open("/tmp/temp", O_WRONLY|O_CREAT);
  write(fd, s, sizeof(s));
  close(fd);
  fd = open("/tmp/temp", O_RDONLY);
  size = read(fd, buffer, sizeof(buffer));
  close(fd);
  printf("%s", buffer);
}

perform


Linux Programmer!

C close() function: close the file

The header file:


#include <unistd.h>

Definition function:


int close(int fd);

Function description: close() can be used to close a file if it is no longer needed after using it. Two close() will write the data back to disk and free the resources occupied by the file. Parameter fd is the file descriptor previously returned by open() or creat().

Return value: returns 0 if the file closes successfully and -1 if an error occurs.

Error code: EBADF parameter fd is not a valid file descriptor or the file is closed.

Note: while the system automatically closes opened files at the end of the process, it is recommended that you close the file yourself and indeed check the return value.


Related articles: