Linux file programming C language Operating system call function Summary (i)

Source: Internet
Author: User

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

The Open function call successfully returns the file descriptor of the opened file, which returns 1 on Failure . the Oflag parameter indicates that the status flag of the open file must and can contain only one of the following 5 Flags:

O_rdonly: Read-only open

O_wronly: Write-only open

O_rdwronly: Read and write Open

O_exec: Execute only open

O_search: Only search opens, only for directories

In addition to these flags, there are some flags that are optional options

O_append: write at the end of the file, when this option exists, each time the file is written with the base of the end of the file to add content, can not modify the written content

O_creat: The file does not exist when the file is created

O_EXCL: A call error occurs when the file already exists with o_creat

O_follow: If path is a symbolic link, an error is called

O_trunc: If the file exists, clear the file length to 0 Open

O_sync: Each write operation waits for the physical io operation to complete, including the IO required to update the file attributes (such as file length) caused by the write operation

O_dsync: Each write operation waits for the physical IO operation to complete, but if the write does not cause a change in the file properties, you do not need to wait for the file property file property to be updated.

When you include o_creat in File open mode , you must also provide permissions for the files you create

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

The close function closes the open file, and the open file is closed automatically when the process ends, but the open file should be closed in a timely manner if the process does not end, because the system has a limit on the maximum number of files opened per process

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

Lseekfunction to locate the location of the file,Offsetparameter description file pointer relative towhenceHow many bytes are moved,whenceparameters areSeek_set,seek_cur,seek_endthree symbols,indicates the header, current position, and tail of the file, respectively. When the file isO_appendwhen the argument is open, the three flags for the function are always the same, all of the file tails, and,Offsetcannot be negative, that is, the file that has been written cannot be modified. BecauseLseekof the functionOffsetparameters can be positively negative, soLseekOn failure, the return value is-1, the other return values are likely to be successful.

In addition, theLseek function simply moves the file pointer and does not cause any io Operations, the position of the file pointer is referenced to the next io operation

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

The read function reads the contents of the nbytes byte from the fd file into the buf , and returns the number of bytes read in the success (possibly less than nbytes), the end of the file is returned with a return of 0, 1 on Failure

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

The Write function writes the contents of nbytes bytes in the buf to the fd file, returns the number of bytes written when successful, and returns when failed -1. Typically, the return value is equal to nbytes , or some error has occurred

#include <unistd.h>ssize_t pread (int fd, void *buf, size_t nbytes, off_t offset); ssize_t pwrite (int fd, const void * BUF, size_t nbytes, off_t offset);

Return-1 On Error, number of bytes read / write on success

The Pread function is equivalent to calling Lseek before calling read, but there is an important difference between the two: pread is Atomic, Non-split, while calling Lseek in the call to read Two calls to the intermediate file may be modified by other processes; two is pread do not update the offset of the current file

#include <unistd.h>int dup (int fd), int dup2 (int fd, int fd2);

Returns 1On Error, returns new file descriptor on success

The DUP returns a new file descriptor for the file that is already open, which must be the minimum value in the currently available file descriptor. dup2 uses the FD2 parameter to specify a new file descriptor, and if fd2 refers to a file that is already open, close fd2 the documents referred to; FD equals FD2 , the return FD2 Without shutting it down.

#include <fcntl.h>int fcntl (int fd, int cmd, .../* int arg */);

Return-1 On Error, dependent on cmd when successful

The Fcntl function can get / Modify the properties of the file

cmd = f_dupfd: Copy file descriptor FD, returns the new file descriptor, which is a minimum value greater than or equal to the third parameter in the file descriptor that has not been opened

cmd = f_getfd: return FD

cmd = f_setfd: sets a new file descriptor to the third parameter,

cmd = F_GETFL: Returns the file status flag

cmd = F_SETFL: Set the file status flag (you need to get the old file status flag at this point, and do the bit with the file status flag you need to set | operation, otherwise the old file flag is cleared from all locations


Linux file programming C language Operating system call function Summary (i)

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.