Linux system Programming file and I/O (ii) file read Write

Source: Internet
Author: User

One, read system call

Once you have a file descriptor associated with an open file description, you can read bytes from the file using the read () system call as long as the file is opened with the o_rdonly or O_RDWR flag

Function Prototypes:

ssize_t Read (int fd, void *buf, size_t count);

Parameters:

FD: File descriptor for the file you want to read

BUF: Pointer to memory block, from bytes read in file to this memory block

Count: Number of bytes copied from this file to BUF

return value:

Returns 1 if an error occurs, returns 0, or returns the number of bytes copied from the file to the specified buffer

Second, write system call

Write data to a file using the Write () system call

Function Prototypes:

ssize_t Write (int fd, const void *buf, size_t count);

Function arguments:

FD: File descriptor for the file to be written

BUF: Pointer to memory block, read data from this memory block to write to file

Count: Number of bytes to write to file

Return value: Returns-1 If an error occurs, and returns the number of bytes written to the file if the write succeeds

Three, IOCTL function

IOCTL is used to send control and configuration commands to devices, some commands also need to read and write some data, but these data cannot be read and written by Read/write, called Out-of-band data. That is, the data read and written by Read/write is In-band data, the main body of I/O operations, and the IOCTL command transmits control information, where the data is auxiliary data. For example, in the serial port to send and receive data through the read/write operation, while the serial port baud rate, check bit, stop bit through the IOCTL set, A/D conversion results through read reading, and A/D conversion accuracy and working frequency through IOCTL settings.

#include <sys/ioctl.h>

int ioctl (int d, int request, ...);

D is a file descriptor for a device. The request is a IOCTL command, and the variable parameter depends on the request, usually a pointer to a variable or structure body. If the error returns-1, if successful returns the other value, the return value also depends on the request.

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.