linux system programming: comprehensive application of read,write and lseek

This example performs the corresponding reading operation according to the command line parameters:

Usage:

usage:./io file {r<length>|R<length>|w<string>|s<offset>}...

File parameter: file name, if it does not exist, it will be created automatically

r < length >: for example, r5, r: read operation, which means to read 5 bytes after the current file pointer and output in text form

R < length >: for example, R5 R: read operation means to read 5 bytes after the current file pointer and output in hexadecimal form

w < string >: for example, wghostwu: w means write operation, which means write 5 bytes after the current file pointer

S < offset >: for example, s1000, move the pointer 1000 bytes from the beginning of the file

Source code:

  1 /*================================================================
  2 *   Copyright (C) 2018 . All rights reserved.
  3 *   
  4 *   File name: io.c
  5 *   Creator: ghostwu (Wu Hua)
  6 *   Date of establishment: January 10, 2018
  7 *   Description: write,open,lseek combination example
  8 *
  9 ================================================================*/
 10 
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #include <sys/types.h>
 15 #include <sys/stat.h>
 16 #include <fcntl.h>
 17 #include <limits.h>
 18 #include <sys/types.h>
 19 #include <unistd.h>
 20 
 21 //Parameter string to integer
 22 int str_to_long( char* str );
 23 
 24 int main(int argc, char *argv[])
 25 {
 26     int i = 2;
 27     int fd = -1;
 28     //Save the result of string reshaping
 29     int res;
 30     //Bytes written
 31     ssize_t num;
 32     //Dynamically allocated heap memory
 33     char* buf;
 34     //Bytes read
 35     int numread;
 36 
 37     if( argc < 3 || strcmp( argv[1], "--help" ) == 0 )  {
 38         printf( "usage:%s file {r<length>|R<length>|w<string>|s<offset>}...\n", argv[0] );
 39         exit( -1 );
 40     }
 41 
 42     fd = open( argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
 43     if( fd < 0 ) {
 44         printf( "file%s Failed to open or create", argv[1] );
 45         exit( -1 );
 46     }
 47     
 48     for( i = 2; i < argc; i++ ){
 49         switch( argv[i][0] ){ 
 50             //Move pointer, s Bytes moved after
 51             case 's':
 52                 res = str_to_long( &argv[i][1] );
 53                 if( -1 == res ) {
 54                     printf( "character string->Plastic conversion failed\n" );
 55                     exit( -1 );
 56                 }
 57                 lseek( fd, res, SEEK_CUR );    
 58                 printf( "%s--->Pointer moved successfully\n", argv[i] );
 59                 break;
 60             //write file, w Content written after
 61             case 'w':
 62                 num = write( fd, &argv[i][1], strlen( &argv[i][1] ) );
 63                 if( num == -1 ) {
 64                     printf( "%s Write failed\n", argv[i] );
 65                 }
 66                 printf( "%s Write succeeded%ld Bytes\n", argv[i], num );
 67                 break;
 68             case 'r': //Character output
 69             case 'R': //Hex output
 70                 res = str_to_long( &argv[i][1] );
 71                 if( -1 == res ) {
 72                     printf( "character string->Plastic conversion failed\n" );
 73                     exit( -1 );
 74                 }
 75                 buf = malloc( res );
 76                 if( buf == NULL ){
 77                     printf( "memory allocation failed" );
 78                     exit( -1 );
 79                 }
 80                 numread = read( fd, buf, res );
 81                 if( -1 == numread ) {
 82                     printf( "Data read failed\n" );
 83                     exit( -1 );
 84                 }
 85                 if( 0 == numread ) {
 86                     printf( "End of file reached" );
 87                 }else {
 88                     printf( "%s: ", argv[i] );
 89                     for ( int j = 0 ; j < numread; j ++ ){
 90                         if( 'r' == argv[i][0] ) {
 91                             printf( "%c", buf[j] );
 92                         }else {
 93                             printf( "%02x ", buf[j] );
 94                         }
 95                     }
 96                 }
 97                 break;
 98             default:
 99                 printf( "parameter%s Must use[rRws]A beginning in\n", argv[i] );
100         }
101     }
102 
103     return 0;
104 }
105 
106 int str_to_long( char* str ) {
107     char* endstr;
108     int res;
109     res = strtol( str, &endstr, 10 );
110     if( (res == LONG_MIN) || (res == LONG_MAX) ) {
111         return -1;
112     }
113     return res;
114 }

Complete presentation:

 1 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls
 2 cp  cp.c  cp.c.copy  io  io.c  strtol  strtol.c
 3 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ !g
 4 gcc io.c -o io
 5 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
 6 total 56
 7 -rwxrwxr-x 1 ghostwu ghostwu  9016 1 month  10 17:16 cp
 8 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:15 cp.c
 9 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:16 cp.c.copy
10 -rwxrwxr-x 1 ghostwu ghostwu 13360 1 month  10 22:19 io
11 -rw-rw-r-- 1 ghostwu ghostwu  2743 1 month  10 22:19 io.c
12 -rwxrwxr-x 1 ghostwu ghostwu  8824 1 month  10 20:47 strtol
13 -rw-rw-r-- 1 ghostwu ghostwu   616 1 month  10 20:47 strtol.c
14 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt
15 usage:./io file {r<length>|R<length>|w<string>|s<offset>}...
16 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
17 total 56
18 -rwxrwxr-x 1 ghostwu ghostwu  9016 1 month  10 17:16 cp
19 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:15 cp.c
20 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:16 cp.c.copy
21 -rwxrwxr-x 1 ghostwu ghostwu 13360 1 month  10 22:19 io
22 -rw-rw-r-- 1 ghostwu ghostwu  2743 1 month  10 22:19 io.c
23 -rwxrwxr-x 1 ghostwu ghostwu  8824 1 month  10 20:47 strtol
24 -rw-rw-r-- 1 ghostwu ghostwu   616 1 month  10 20:47 strtol.c
25 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 wghostwu
26 s1000--->Pointer moved successfully
27 wghostwu 7 bytes successfully written
28 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
29 total 60
30 -rwxrwxr-x 1 ghostwu ghostwu  9016 1 month  10 17:16 cp
31 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:15 cp.c
32 -rw-rw-r-- 1 ghostwu ghostwu  1752 1 month  10 17:16 cp.c.copy
33 -rwxrwxr-x 1 ghostwu ghostwu 13360 1 month  10 22:19 io
34 -rw-rw-r-- 1 ghostwu ghostwu  2743 1 month  10 22:19 io.c
35 -rwxrwxr-x 1 ghostwu ghostwu  8824 1 month  10 20:47 strtol
36 -rw-rw-r-- 1 ghostwu ghostwu   616 1 month  10 20:47 strtol.c
37 -rw-rw-r-- 1 ghostwu ghostwu  1007 1 month  10 22:20 test.txt
38 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt r1007
39 r1007: ghostwughostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt R1007
40 R1007: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 67 68 6f 73 74 77 75 
41 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 r7 42 s1000--->Pointer moved successfully 43 r7: ghostwughostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 R7 44 s1000--->Pointer moved successfully

Tags: Linux Ubuntu

Posted on Tue, 05 May 2020 09:52:15 -0400 by abie10