Common Linux C functions: open, read, and write

Source: Internet
Author: User
Http://blog.chinaunix.net/u2/82646/showart_1359552.html

Open (open a file)
Related functions: read, write, fcntl, close, Link, stat, umask, unlink, and fopen
Header file # include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Define the int open (const char * pathname, int flags) function );
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 flags that can be used by the flags parameter:
O_rdonly open a file in read-only mode
O_wronly open the file in write-only mode
O_rdwr can open files in read/write mode. 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 is automatically created.
O_excl if o_creat is also set, this command checks whether the file exists. If the file does not exist, the file will be created; otherwise, the file will be opened incorrectly. In addition, if both o_creat and o_excl are set and the file to be opened is a symbolic connection, opening the file will fail.
If the file to be opened is a terminal device, o_noctty does not regard the terminal as a process control terminal.
If the o_trunc file exists and is opened in writable mode, 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 appended to the end of the file.
O_nonblock open the file in an unblocking way, that is, whether there is data read or waiting, it will immediately return to the process.
O_ndelay is the same as o_nonblock.
O_sync opens the file in synchronous mode.
O_nofollow if the file indicated by pathname is a symbolic connection, opening the file will fail.
O_directory if the file indicated by the pathname parameter is not a directory, opening the file will fail.
This is a flag unique after linux2.2 to avoid some system security problems. The mode parameter has the following combinations. This parameter takes effect only when a new file is created. In addition, the permissions for creating a file are affected by the umask value, therefore, the file permission should be (Mode-umaks ).
S_irwxu00700 permission indicates that the object owner has the readable, writable, and executable permissions.
S_irusr, s_iread, and 00400 permissions indicate that the file owner has the readable permission.
S_iwusr or s_iwrite, 00200 permission indicates that the file owner has the write permission.
S_ixusr or s_iexec, 00100 permission indicates that the file owner has executable permission.
S_irwxg 00070 permission indicates that the file user group has the readable, writable, and executable permissions.
S_irgrp 00040 permission indicates that the file user group has the readable permission.
S_iwgrp 00020 permission indicates that the file user group has the write permission.
S_ixgrp 00010 permission indicates that the file user group has executable permissions.
S_irwxo 00007 indicates that other users have the readable, writable, and executable permissions.
S_iroth 00004 permission, which indicates that other users have the readable permission
S_iwoth 00002 permission indicates that other users have the write permission.
S_ixoth 00001 permission indicates that other users have executable permissions.

Return Value: if all the permissions to be verified have passed the check, 0 is returned, indicating success. If one permission is disabled,-1 is returned.

ErrorCodeThe file indicated by the eexist parameter pathname already exists, but the o_creat and o_excl flag are used.
The file indicated by the eaccess parameter pathname does not meet the required permissions.
The file to be tested by erofs is stored in the read-only file system.
The pathname pointer of the efault parameter exceeds the accessible memory space.
The Mode Val parameter mode is incorrect.
The pathname parameter of enametoolong is too long.
The pathname parameter of enotdir is not a directory.
The enomem core memory is insufficient.
The pathname parameter of eloop has too many symbolic connections.
Eio I/O access error.

Note that you must be especially careful when using access () for user authentication. For example, making an open () empty file after access () may cause system security problems.

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

Run Linux programmer!

Read (read data from opened files)
Related functions: readdir, write, fcntl, close, lseek, readlink, and fread
Header file # include <unistd. h>
Define the function ssize_t read (int fd, void * Buf, size_t count );
Function Description: Read () transfers the file referred to by the FD parameter to the memory indicated by the Buf pointer in count bytes. If the Count parameter is 0, read () does not work and 0 is returned. The returned value is the number of bytes actually read. If 0 is returned, it indicates that the data has reached the end of the file or cannot be read. In addition, the read/write location of the file will move with the read bytes.

Additional instructions: If read () is successful, the actual number of bytes read will be returned. It is best to compare the returned value with the parameter count. If the returned number of bytes is less than the required number of bytes, it is possible to read the end of the file, read from the pipe or terminal, or read () is interrupted by the signal. If an error occurs,-1 is returned. The error code is stored in errno, and the file read/write location is unpredictable.

Error Code eintr this call is interrupted by the signal.
Eagain: When I/O cannot be blocked (o_nonblock), this value is returned if no data can be read.
The ebadf parameter FD is a non-valid file description word, or the file is closed.

For an example, see open ().

Sync (write buffer data back to disk)
Related Function fsync
Header file # include <unistd. h>
Define function int sync (void)
Function Description sync () writes the System Buffer data back to the disk to ensure data synchronization.
The return value is 0.

Write (write data into an opened file)
Related functions: open, read, fcntl, close, lseek, sync, fsync, and fwrite
Header file # include <unistd. h>
Define the function ssize_t write (int fd, const void * Buf, size_t count );
Function Description: Write () writes the memory referred to by the parameter Buf to the file referred to by the parameter FD in count bytes. Of course, the file read/write location will also move.
If the return value is successfully written (), the actual number of bytes written will be returned. If an error occurs,-1 is returned, and the error code is stored in errno.
Error Code eintr this call is interrupted by the signal.
Eagain: When I/O cannot be blocked (o_nonblock), this value is returned if no data can be read.
The EADF parameter FD is a non-valid file description word, or the file is closed.
For examples, see open ().

Original article addressHttp://hi.baidu.com/firstm25/blog/item/7db32c3de5ba0ec29e3d6251.html

Related Article

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.