What about Linux (5)-programming files in Linux I/O

Source: Internet
Author: User

Date: 2009.05.08

Content: file I/O

General description of available file I/O functions: open files, read files, write files, etc. file I/O generally includes five functions: Open/read/write/lseek/close

The function described in this section is often called I/O without buffering. It refers to a system call in the kernel that every read and write calls.

As long as resources are shared among multiple processes, the concept of atomic operations is very important.

 

1. Open Functions

You can call the OPEN function to open or create a file.

**************************************** ****************************************

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

The Oflag parameter can be used to describe multiple options of this function (o_rdonly indicates read-only open; o_wronly indicates write-only open; o_rdwr indicates read-write open)

The three constants must be specified and can only be specified. Some constants are optional and are not given here.

The file descriptor returned by open must be the smallest unused descriptor value.

**************************************** ****************************************

# Include <fcntl. h> <br/> int open (const char * pathname, int Oflag ,... /* mode_t mode */); <br/> // return value: if a file descriptor is returned successfully,-1 is returned if an error occurs.

2. creat Function

Call the creat function to create a file.

# Include <fcntl. h> <br/> // return value: Same as open <br/> int creat (const char * pathname, mode_t mode); <br/>

3. Close Function

Call the close function to close an opened file.

# Include <unistd. h> <br/> // return value: If 0 is returned successfully,-1 is returned. <br/> int close (INT filedes) is returned );

4. lseek Function

Call the lseek function to explicitly set the offset for an opened file.

**************************************** ****************************************

The description of offset is related to the whence parameter.

If whence is seek_set, set the offset of the file to offset bytes from the beginning of the file.

If the whence is seek_cur, set the offset of the file to the current value plus offset. The offset can be positive or negative.

If the whence is seek_end, set the offset of the file to the file length plus offset. The offset can be positive or negative.

**************************************** ****************************************

# Include <unistd. h> <br/> // if a new file offset is returned successfully,-1 is returned if an error occurs. <br/> off_t lseek (INT filedes, off_t offset, int whence) is returned ); <br/>

 

Instance 3-1 test whether the offset can be set for the standard input

# Include "apue. H "</P> <p> int <br/> main (void) <br/> {<br/> If (lseek (stdin_fileno, 0, seek_cur) =-1) <br/> printf ("cannot seek/N"); <br/> else <br/> printf ("Seek OK/N "); <br/> exit (0); <br/>}

 

 

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.