A summary of common file IO for Unix system interface

Source: Internet
Author: User

The first is commonly used by several functions open, Read,write,lseek, close

Open function function prototype int open (char * path,int oflag,...) The return value is a file descriptor. Path is the name of the filename oflage file is open by the third parameter is used to create a file using/* When creating the file actually there is also a CREATE function to use and openat because it has not been used this function and open differences, so it is not cumbersome here */Open function pay attention to small details when judging with if

Read function prototype ssize_t read (int fd, void* buf, size_t nbytes) The return value is a file read byte number in several cases the return value is not equal to the number of file read bytes that is the third parameter nbyt The case of es second parameter buf read to BUF memory file offset (current file offset) is changed

Write function prototype ssize_t write (int fd, const void* buf, size_t nbytes) The return value is the file write byte number fd is the file descriptor that writes buf content to nbytes bytes to a file But it's important to note that the default is to wait for a write in the system queue (different opening methods will be different)//three errors are returned-1

Lseek function off_t lseek (int fd, off_t offset, int whence) return value success function returns a new file offset failure-1 FD file Descriptor off_t is a signed integer whence is actually and off_t package Use the Seek_set file at the beginning seek_cur the relative position of the current value seek_end file length +-

Close function prototype int close (int fd) return value successfully returned 0 failed-1 close file descriptor

Here are two important concepts. One is that the kernel uses three data structures to represent open files and another reason is that the atomic nature of file IO

Because there is no drawing software in the Linux environment to describe three kinds of file state data structure The first is a description of the file descriptor fd (I'm lazy, I prefer to know when the principle is used to check the details = =!! The second is the file table entry The third is the V node the first second one is a linked list

Then the file table entry points to the V node different FD file descriptors access the same file is access different FD point to the same V node

1 structfd{2      intFd//File Descriptor3      void* FILEPTR;//The file pointer points to the file table4 };5 6 7 structfiletable{8      intFilemark;//file Status Flag9off_t offset;//file OffsetTen      void*Vnode;  One } AActually, there's a third one I didn't learn, so I'm not a pest.

After reading this very broken code (the name is not capitalized), you should find a problem if two threads access the same file will produce an operation exception is actually the data of the individual File table entry asynchronous cause may cause the data to overwrite another process the necessity of atomic operation is represented here. It means that all operations are done one time. The operating system does not execute the other process in half.

fcntl function int fcntl (int fd, int cmd,...) This function can change the properties of a file that has already been opened, but note that you must first obtain the current flag value and then set the code to apply to one to create the file to read-only open using FCNTL to modify the open state to write text

1 #include <apue.h>
2 #include <fcntl.h>
3 void Set_fl (int fd, int flags)//flags is file status to Ture on
6 9
5 int Val;
6 if (val = fcntl (FD,F_GETFL, 0)) < 0) {
7 printf ("err-1!");
8 Err_sys ("Fcntl get error!");
9}
Ten Val |= flags;
if (Fcntl (FD, F_SETFL, Val) < 0) {
printf ("Error 0!");
Err_sys ("Fcontl set error!");
14}
15
16}
17
int main (void)
19 {
int fd = 0;
+ char buf[] = "Xiaochen";
22
if (FD = open ("Aa.txt", O_wronly)) < 0) {
printf ("Err 1!");
Err_sys ("Open error!");
26}
SET_FL (fd,o_wronly);
if (Write (FD, BUF, 8)! = 8) {
printf ("Err 2!");
Err_sys ("write error");
31}
32
Close (FD);
return 0;
35
36}
~                                                                                                                                                                                                                                                        
~

Because just summed up not doing too much detail description

Finish.... Books read a lot less than just know the concept of the above functions have a simple demo I am more lazy code will not show the end of a chapter to do a small summary or very useful if you see this broken article one day to study the problem or found in the blog in my error please contact me very much thank you QQ : 1639153000

A summary of common file IO for Unix system interface

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.