File I/O, file I

Source: Internet
Author: User

File I/O, file I

File descriptor

For the kernel, all opened files are referenced through the file descriptor. The file descriptor is a non-negative integer. By default, each process has three open file descriptors associated with them, namely, standard input 0, standard output 1, and standard error 2. The POSIX specification defines three symbolic constants STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO in the header file unistd. h.

The number of files that can be opened by any process at the same time is limited. This limit is usually caused by limits. the value of the constant OPEN_MAX defined in the h header file varies with the system, but the POSIX specification requires that it be at least 16.

 

Open Function

#include <fcntl.h>int open(const char *pathname, int oflag, ... /* mode_t mode */);

The open function is used to open or create a file. If a file descriptor is successfully returned,-1 is returned.

Pathname is the name of the file to be opened or created.

The oflag parameter is the result of the bitwise OR operation of one or more of the following constants.

  • O_RDONLY read-only
  • O_WRONLY write-only open
  • O_RDWR read/write Enabled

The preceding three constants must be specified and can only be specified. The following constants are optional:

  • O_APPEND: writes the data to the end of the file.
  • O_CREAT: if the file does not exist, create it. When this option is used, the third parameter mode is required to specify the access limit for the new file.
  • O_EXCL if O_CREAT is specified and the file already exists, an error will occur.
  • O_TRUNC if the file exists and is successfully opened in write-only or read/write mode, the length of the file is truncated to 0.
  • O_NOCTTY if pathname refers to a terminal device, the device is not allocated as the control terminal of the process.
  • O_NONBLOCK if pathname refers to a FIFO file, block device file, or character device file, this option sets the file opening operation and subsequent I/O operations to non-blocking mode.

The following three logos are also optional. They are part of the synchronous Input and Output options in Single UNIX Specification (and POSIX.1:

  • O_DSYNC enables each write operation to wait for the completion of the physical I/O operation. However, if the write operation does not affect reading the newly written data, the file attribute is not updated.
  • O_RSYNC waits for each read operation with the file descriptor as the parameter until any unfinished write operation on the same part of the file is completed.
  • O_SYNC makes every write wait for the completion of the physical I/O operation, including the I/O Required for updating the file attributes caused by the write operation

The mode parameter is used only when the oflag parameter specifies the O_CREAT option. It is used to specify the access limit for the new file. These flags are defined in the sys/stat. h header file:

  • S_IRUSR read permission, file owner
  • S_IWUSR write permission, file owner
  • S_IXUSR execution permission, file owner
  • S_IRGRP read permission, file group
  • S_IWGRP write permission, file group
  • S_IXGRP execution permission, group to which the file belongs
  • S_IROTH read permission, other users
  • S_IWOTH write permission, other users
  • S_IROTH execution permission, other users

There are several factors that may affect the access permissions of files. First, the specified access permission is only used when a file is created. Secondly, the user mask will affect the access permission of the created file. That is to say, the result of the AND operation between the mode value given by open AND the inverse value of the user mask, is the true access permission of the file. The file descriptor returned by open must be the smallest unused descriptor value.

 

Creat Function

#include <fcntl.h>int creat(const char *pathname, mode_t mode);

You can also call the creat function to create a new file. This function is equivalent to: open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode). If the function is successfully executed, the file descriptor is returned, otherwise,-1 is returned.

One disadvantage of creat is that it opens the created file in write-only mode.

 

Close Function

#include <unistd.h>int close(int filedes);

The close function closes an open file and returns 0 if execution is successful. Otherwise,-1 is returned.

When you close a file, all the locks that the process adds to the file are also released. When a process is terminated, the kernel automatically closes all open files.

 

Lseek Function

#include <unistd.h>off_t lseek(int filedes, off_t offset, int whence);

Each opened file has an offset associated with the current file. It is usually a non-negative integer used to measure the number of bytes calculated at the beginning of the growth. Generally, read and write operations start from the offset of the current file and increase the number of bytes read and write. By default, when a file is opened, the offset is set to 0 unless the O_APPEND option is specified.

You can call lseek to set its offset for an opened file. The description of the parameter offset is related to the value of the parameter whence:

  • If whence is SEEK_SET, the offset of the file is set to offset bytes from the beginning of the file.
  • If whence is SEEK_CUR, the offset of the file is set to the current value plus offset, and the offset can be positive or negative.
  • If the whence is SEEK_END, the offset of the file is set to the file length plus offset, and the offset can be positive or negative.

If lseek is successfully executed, a new file offset is returned; otherwise,-1 is returned.

Generally, the current offset of a file should be a non-negative integer, but some devices may also allow a negative offset. However, for normal files, the offset must be non-negative. Because the offset value may be a negative value, be cautious when comparing the return value of lseek. Do not test whether it is less than 0, but test whether it is equal to-1.

Lseek only records the current file offset in the kernel and does not cause any I/O operations. The offset is then used for the next read or write operation.

The file offset can be greater than the current length of the file. In this case, the next write to the file will lengthen the file and create a hole in the file, all bytes in the file that have not been written are read as 0.

 

Read Function

#include <unistd.h>ssize_t read(int filedes, void *buf, size_t nbytes);

Call the read function to read data from open files. If the execution is successful, the number of bytes read is returned. If the execution is successful, 0 is returned and-1 is returned if an error occurs.

 

Write function

#include <unistd.h>ssize_t write(int filedes, const void *buf, size_t nbytes);

Call the write function to write data to open files. The number of bytes written after successful execution. If an error occurs,-1 is returned.

The returned value is usually the same as the value of the parameter nbytes. Otherwise, an error occurs.

 

The following is an example program used to demonstrate the usage of underlying I/O functions.

 1 #include <unistd.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <stdlib.h> 5  6 int main(void) 7 { 8     char block[4096]; 9     int in, out;10     int nread;11 12     in  = open("file.in", O_RDONLY);13     out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP);14     while ((nread = read(in, block, 4096)) > 0) {15         write(out, block, nread);16     }17 18     exit(0);19 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.