Linux system Programming-file IO functions

Source: Internet
Author: User


First, the IOCTL function

The IOCTL is used to control and configure commands to the device, and some commands also need to read and write some data, but the data cannot be read and written by Read/write, known as out-of-band data. That is, Read/write read and write data is In-band data, is the main body of the I/O operation, and the IOCTL command transmits control information, in which the data is the auxiliary data. For example, on the serial line to send and receive data through the read/write operation, and the serial port baud rate, check bit, stop bit through the IOCTL settings, A/D conversion results through read read, and A/D conversion accuracy and operating frequency through the IOCTL set.

#include <sys/ioctl.h>

int ioctl (int d, int request, ...);
D is a file descriptor for a device. Request is the command of the IOCTL, and the mutable parameter depends on the request, usually a pointer to a variable or struct body. Returns 1 if an error occurs, and returns the other value if successful, and the return value is dependent on request.
The following program uses the Tiocgwinsz command to obtain the window size of the end device.

#include <stdio.h> #include <stdlib.h#include <unistd.h> #include <sys/ioctl.hint main (void) {
     struct winsize size;  Property Structure Body    if (isatty (stdout_fileno) = = 0)         exit (1);    if (IOCTL (Stdout_fileno, Tiocgwinsz, &size) < 0)    {        perror ("IOCTL Tiocgwinsz error");        Exit (1);    }    printf ("%d rows,%d columns\n", Size.ws_row, Size.ws_col);    return 0;}

in the terminal of the graphical interface to change the size of the terminal window several times and run the program, observe the results.


Ii. random reading and writing of documents

All the file accesses so far are sequential access. This is because all of the read and write starts at the offset of the current file, and then the file offset value is automatically added to the location just beyond the end of the read or write, making it ready for the next visit.
There is a file migration mechanism, in Linux system, random access becomes very simple, all you need to do is to change the current file offset value to the relevant location, it will force the next read () or write () in this position. (unless the file is opened with a O_append flag, in which case any write call will still occur at the end of the file)

Lseek system Call:

Function Description: Reposition by specifying the number of bytes relative to the start position, current position, or end position, depending on the location specified in the Lseek () function
Function prototypes: off_t lseek (int fd, off_t offset, int base);

Function parameters:

FD: File descriptor that needs to be set

Offset: Offsets

Base: Offset base position

Return value: Returns the new file offset value

Base represents the starting position of the search with the following values: (These values are defined in <unistd.h>)

Base file location

Seek_set Calculating offsets from the beginning of a file
Seek_cur to calculate offsets from the current file's offset value
Seek_end calculate offset from end of file


Note: The pipe and socket are not lseek, otherwise the espipe error (Invalid seek) is returned.


Create an empty file experiment

The sample program is as follows:

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include         <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define ERR_EXIT (m) do {         Perror (m);     Exit (Exit_failure);    } while (0) int main (int argc, char *argv[]) {int infd;    int outfd;        if (argc! = 3) {fprintf (stderr, "Usage%s src dest\n", argv[0]);    Exit (Exit_failure);    } INFD = open (argv[1], o_rdonly);    if (INFD = =-1) err_exit ("Open src error"); if (outfd = open (argv[2], o_wronly | O_creat |    O_trunc, 0664)) = =-1) err_exit ("Open dest Error");    Char buf[1024];    ssize_t nread; while ((Nread = Read (INFD, buf, 1024x768)) > 0) write (outfd, buf, nread);    You can call Fsync to synchronize the kernel buffer data to the disk File//or open the file with the flag O_sync Close (INFD);    Close (OUTFD);    int fd = open ("Test.txt", o_rdonly);    if (fd = =-1) err_exit ("Open error");    Char buf2[1024] = {0}; int ret = read (FD, BUF2, 5);    if (ret = =-1) err_exit ("read error"); ret = Lseek (FD, 0, seek_cur);    Offset 0 bytes from the current position if (ret = =-1) err_exit ("Lseek");    printf ("Current offset=%d\n", ret); FD = open ("Hole.txt", O_wronly | O_creat |    O_trunc, 0664);    if (fd = =-1) err_exit ("Open error");    Write (FD, "ABCDE", 5);    ret = Lseek (FD, 1012 * 1024x768, seek_cur);    if (ret = =-1) err_exit ("Lseek error");    Write (FD, "Hello", 5);    /* The empty characters in the middle do not occupy disk space, such as Ls-lh hole.txt and du-h hole.txt * See the file size is not the same */close (FD); return 0;}


Linux system Programming-file IO functions

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.