[Study Notes] file I/O operations on the Linux platform

Source: Internet
Author: User

The following functions are I/O (unbuffered I/O) functions without Caching:

For the kernel, all opened files are described by a non-negative integer (called a file descriptor ).

The following describes the file I/O functions:

(1) Open

This function is used to open or create a file. If the operation succeeds, the system returns the file descriptor. If the operation fails, the system returns-1.

Format:

# Include <sys/types. h>

# Include <sys/STAT. h>

# Include <fcntl. h>

Int open (const char * Name, int Oflag [| optional parameter] [, mode]);

(2) creat

This function creates a new file. If the operation succeeds, the system returns the file descriptor. If the operation fails, the system returns-1.

Format:

# Include <sys/types. h>

# Include <sys/STAT. h>

# Include <fcntl. h>

Int creat (const char * Name, mode_t mode );

(3) Close

This function is used to close a file that has already been called.

Format:

# Include <unistd. h>

Int close (INT file );

0 is returned. Otherwise,-1 is returned.

(4) lseek

This function is used to specify the displacement of the current file. This displacement is a non-negative integer used to indicate the number of bytes calculated from the beginning of the file.

Format:

# Include <unistd. h>

# Include <sys/types. h>

Off_t lseek (INT file, off_t offset, int whence );

The file displacement is returned successfully;-1 is returned if an error occurs.

(5) Reed

This function reads data from a file.

Format:

# Include <unistd. h>

Size_t read (INT file, void * Buf, size_t bytes );

Returns the byte book. If it has been read to the end, 0 is returned. If an error occurs,-1 is returned.

(6) Write

This function writes data to a file.

Format:

# Include <unistd. h>

Size_t write (INT file, void * buff, size_t bytes );

Returns the total number of bytes written to the file. The size must be smaller than the buffer capacity. -1 is returned if an error occurs.

(7) DUP

This function can be used to copy an existing file descriptor.

Format:

# Include <unistd. h>

Int DUP (INT oldfile );

Int duo2 (INT oldfile, int newfile); // you can specify the descriptor of the new file.

The file descriptor of the new file is returned successfully;-1 is returned if the file fails to be returned.

(8) fcntl

Purpose 1: copy the descriptor (cmd = f_duped)

Purpose 2: Get/set the identifier of the file's love Descriptor

Purpose 3: Get the File status flag

Purpose 4: Get/set the record lock

Purpose 5: Obtain/set asynchronous I/O

Format:

# Include <unistd. h>

# Include <fcntl. h>

Int fcntl (int fd, int cmd );


Int fcntl (int fd, int cmd, long Arg );


Int fcntl (

Int FD, int cmd, struct flock * Lock );

(9) IOCTL

This function is short for input/output control, that is, input/output control. It can control character devices through a file descriptor.

Format:

# Include <sys/IOCTL. h>

Int IOCTL (INT file, int request, char * argp | struct termios st );

0 is returned for success, and-1 is returned for error.

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.