SoFunction
Updated on 2025-03-05

Basic functions for operating files using C language

C language creat() function: create file function

Header file:

#include <sys/>  #include <sys/>  #include <>

Define the function:

int creat(const char * pathname, mode_tmode);

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

Error code: For parameter mode, please refer to the open() function.

Return value:

  • creat() will return a new file descriptor. If an error occurs, it will return -1, and set the error code to errno.
  • EEXIST parameter: The file referred to by pathname already exists.
  • EACCESS parameter: pathname The file specified does not meet the required permissions for testing
  • EROFS: The file that wants to open write permissions exists in the read-only file system
  • EFAULT parameter: pathname pointer exceeds accessible memory space
  • EINVAL parameter: mode is incorrect.
  • ENAMETOOLONG parameter: pathname is too long.
  • ENOTDIR Parameter: pathname is a directory
  • ENOMEM: Core memory insufficient
  • ELOOP parameter: pathname has too many symbolic connection problems.
  • EMFILE: The maximum number of files that can be opened at the same time by the process has been reached.
  • ENFILE: The maximum number of files that can be opened at the same time in the system has been reached.

Additional Note: Creat() cannot create a special device file. Please use mknod() if necessary.


C language open() function: Open file function

Header file:

#include <sys/>  #include <sys/>  #include <>

Define the function:

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

Function description:

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

  • O_RDONLY Open the file read-only
  • O_WRONLY Open file in write-only
  • O_RDWR opens a file in a readable and writeable manner. The above three flags are mutually exclusive, that is, they cannot be used at the same time, but can be combined with the following flags using the OR(|) operator.
  • O_CREAT If the file to be opened does not exist, the file will be automatically created.
  • O_EXCL If O_CREAT is also set, this command will check whether the file exists. If the file does not exist, create the file, otherwise it will cause an error in opening the file. In addition, if O_CREAT and O_EXCL are set at the same time, and the file to be opened is a symbolic connection, the file opening will fail.
  • O_NOCTTY If the file to be opened is a terminal device, the terminal will not be treated as a process control terminal.
  • O_TRUNC If the file exists and is opened in a writable manner, this flag will clear the file length to 0, and the information originally stored in the file will also disappear.
  • O_APPEND When reading and writing a file, it will start to move from the end of the file, that is, the written data will be added to the file in an additional way.
  • O_NONBLOCK opens the file in an unblockable manner, which means that it will immediately return to the process regardless of whether there is data read or waiting.
  • O_NDELAY same as O_NONBLOCK.
  • O_SYNC opens the file in a synchronous manner.
  • O_NOFOLLOW If the file referred to by the parameter pathname is a symbolic connection, the file opening will fail.
  • O_DIRECTORY If the file referred to by the parameter pathname is not a directory, it will fail to open the file. Note: This is a unique flag after Linux2.2 to avoid some system security issues.

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 permissions for the file should be (mode-umaks).

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

Return value: If all permissions to be checked pass the check, a value of 0 will be returned, indicating success. If a permission is prohibited, it will 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 meet the required permissions for testing.
  • EROFS The file to test that write permissions exist in the read-only file system.
  • The EFAULT parameter pathname pointer exceeds the accessible memory space.
  • The EINVAL parameter mode is incorrect.
  • ENAMETOOLONG parameter pathname is too long.
  • The ENOTDIR parameter pathname is not a directory.
  • ENOMEM core memory is insufficient.
  • There are too many symbolic connection problems with the ELOOP parameter pathname.
  • EIO I/O access error.

Additional Note: Be especially careful when using access() for user authentication. For example, opening() and empty file after access() may cause system security problems.

example

#include <>
#include <sys/>
#include <sys/>
#include <>
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);
}

implement

Linux Programmer!

C language close() function: close the file

Header file:

#include <>

Define the function:

int close(int fd);

Function description: If you are no longer needed after using the file, you can use close() to close the file. The second close() will write the data back to disk and release the resources occupied by the file. The parameter fd is the file descriptor previously returned by open() or creat().

Return value: If the file is closed successfully, it will return 0, and if an error occurs, it will return -1.

Error code: EBADF parameter fd is not valid file description word or the file is closed.

Additional Note: Although the system will automatically close the opened file at the end of the process, it is still recommended to close the file yourself and do check the return value.