1. Read system call
Once a file descriptor is connected to an open file description, as long as the file is opened with the o_rdonly or o_rdwr mark, you can use the read () System Call to read bytes from the file.
Function prototype:
Ssize_t read (int fd, void * Buf, size_t count );
Parameters:
FD: file descriptor of the file to be read
Buf: pointer to the memory block. The Bytes read from the file are placed in this memory block.
Count: number of bytes copied from the file to the Buf
Return Value:
If an error occurs,-1 is returned. If the read object ends, 0 is returned. Otherwise, the number of bytes copied from the file to the specified buffer zone is returned.
Ii. Write System Call
Write data to a file using a write () system call
Function prototype:
Ssize_t write (int fd, const void * Buf, size_t count );
Function parameters:
FD: file descriptor of the file to be written
Buf: pointer to the memory block, which reads data from the block and writes it to the file.
Count: number of bytes to write to the file
Returned value: if an error occurs,-1 is returned. If the write is successful, the number of bytes written to the file is returned.
Iii. IOCTL Functions
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 using read/write, which is called out-of-band data. That is to say, the read/write data is in-band data and the main body of the I/O operation, while the ioctl command transfers control information, where the data is auxiliary data. For example, data is sent and received online through read/write operations, while the baud rate, parity bit, and stop bit of the serial port are set through ioctl, and the result of A/D conversion is read through read, the accuracy and frequency of A/D conversion are set through IOCTL.
# Include <sys/IOCTL. h>
Int IOCTL (int d, int request ,...);
D is the file descriptor of a device. Request is an IOCTL command. A variable parameter depends on the request, which is usually a pointer to a variable or struct. If an error occurs,-1 is returned. If the error succeeds, other values are returned. The returned value is also dependent on the request.
The following program uses the tiocgwinsz command to obtain the window size of the terminal device.
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
# Include <stdio. h> # Include <stdlib. h # Include <unistd. h> # Include <sys/IOCTL. h Int main (void) { Struct winsize size; 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; } |
On the GUI terminal, change the terminal window size multiple times and run the program to observe the results.
4. Random file read/write
All file accesses so far are sequential accesses. This is because all the reads and writes start from the offset position of the current file, and then the file offset value is automatically increased to the position just beyond the end of the read or write operation, make it ready for the next visit.
There is a file offset mechanism. In Linux, random access becomes very simple. All you need to do is change the current file value to the relevant location, it will force a read () or write () to occur at this position. (Unless the file is opened by o_append, in this case, any write call will still happen at the end of the file)
Lseek system call:
Function Description: relocate by specifying the number of bytes relative to the start position, current position, or end position, depending on the position specified in the lseek () function.
Function prototype: off_t lseek (int fd, off_t offset, int base );
Function parameters:
FD: file descriptor to be set
Offset: Offset
Base: Offset base position
Return Value: return the new file offset value.
Base indicates the start position of the search, which has the following values: (these values are defined in <unistd. h>)
Base File Location
Seek_set calculates the offset from the beginning of the file
Seek_cur calculates the offset from the offset value of the current file.
Seek_end calculates the offset from the end of the file.
The example program is as follows:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
/*************************************** ********************************** > File name: file_cp.c > Author: Simba > Mail: dameng34@163.com > Created time: sat 23 Feb 2013 02:34:02 pm CST **************************************** ********************************/ # 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, 1024)> 0) Write (outfd, Buf, nread); // you can call fsync to synchronize data in the kernel buffer to disk files. // Or the log is o_sync when the file is opened. 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*1024*1024, seek_cur ); If (ret =-1) Err_exit ("lseek error "); Write (FD, "hello", 5 ); /* The null characters in the middle do not occupy disk space, such as LS-lH hole.txt and Du-H hole.txt. * The file size is different */ Close (FD ); Return 0; } |
You can copy the program to test the actual size of the hosts file.
Reference: apue