這個實例根據命令行參數進行相應的讀學操作: 用法: file參數:文件名, 如果不存在會自動創建 r<length>: 如r5, r: 讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以文本形式輸出. R<length>:如R5 R:讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以十六進位 ...
這個實例根據命令行參數進行相應的讀學操作:
用法:
usage:./io file {r<length>|R<length>|w<string>|s<offset>}...
file參數:文件名, 如果不存在會自動創建
r<length>: 如r5, r: 讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以文本形式輸出.
R<length>:如R5 R:讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以十六進位形式輸出.
w<string>: 如wghostwu: w表示寫入操作,表示在當前文件指針後面寫入5個位元組的內容
s<offset>: 如s1000, 從文件開頭把指針移動1000個位元組
源代碼:

1 /*================================================================ 2 * Copyright (C) 2018 . All rights reserved. 3 * 4 * 文件名稱:io.c 5 * 創 建 者:ghostwu(吳華) 6 * 創建日期:2018年01月10日 7 * 描 述:write,open,lseek結合示例 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 //參數字元串轉整數 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 //保存字元串轉整形的結果 29 int res; 30 //寫入的位元組數 31 ssize_t num; 32 //動態分配的堆記憶體 33 char* buf; 34 //讀取的位元組數 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( "文件%s打開或者創建失敗", argv[1] ); 45 exit( -1 ); 46 } 47 48 for( i = 2; i < argc; i++ ){ 49 switch( argv[i][0] ){ 50 //移動指針, s後面跟移動的位元組數 51 case 's': 52 res = str_to_long( &argv[i][1] ); 53 if( -1 == res ) { 54 printf( "字元串->整形轉換失敗\n" ); 55 exit( -1 ); 56 } 57 lseek( fd, res, SEEK_CUR ); 58 printf( "%s--->指針移動成功\n", argv[i] ); 59 break; 60 //寫入文件, w後面跟寫入的內容 61 case 'w': 62 num = write( fd, &argv[i][1], strlen( &argv[i][1] ) ); 63 if( num == -1 ) { 64 printf( "%s寫入失敗\n", argv[i] ); 65 } 66 printf( "%s成功寫入%ld個位元組\n", argv[i], num ); 67 break; 68 case 'r': //字元輸出 69 case 'R': //十六進位輸出 70 res = str_to_long( &argv[i][1] ); 71 if( -1 == res ) { 72 printf( "字元串->整形轉換失敗\n" ); 73 exit( -1 ); 74 } 75 buf = malloc( res ); 76 if( buf == NULL ){ 77 printf( "記憶體分配失敗" ); 78 exit( -1 ); 79 } 80 numread = read( fd, buf, res ); 81 if( -1 == numread ) { 82 printf( "數據讀取失敗\n" ); 83 exit( -1 ); 84 } 85 if( 0 == numread ) { 86 printf( "已經到達文件尾部" ); 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( "參數%s必須以[rRws]中的一個開頭\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 }View Code
完整的演示效果:
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月 10 17:16 cp 8 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:15 cp.c 9 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:16 cp.c.copy 10 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月 10 22:19 io 11 -rw-rw-r-- 1 ghostwu ghostwu 2743 1月 10 22:19 io.c 12 -rwxrwxr-x 1 ghostwu ghostwu 8824 1月 10 20:47 strtol 13 -rw-rw-r-- 1 ghostwu ghostwu 616 1月 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月 10 17:16 cp 19 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:15 cp.c 20 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:16 cp.c.copy 21 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月 10 22:19 io 22 -rw-rw-r-- 1 ghostwu ghostwu 2743 1月 10 22:19 io.c 23 -rwxrwxr-x 1 ghostwu ghostwu 8824 1月 10 20:47 strtol 24 -rw-rw-r-- 1 ghostwu ghostwu 616 1月 10 20:47 strtol.c 25 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 wghostwu 26 s1000--->指針移動成功 27 wghostwu成功寫入7個位元組 28 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l 29 total 60 30 -rwxrwxr-x 1 ghostwu ghostwu 9016 1月 10 17:16 cp 31 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:15 cp.c 32 -rw-rw-r-- 1 ghostwu ghostwu 1752 1月 10 17:16 cp.c.copy 33 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月 10 22:19 io 34 -rw-rw-r-- 1 ghostwu ghostwu 2743 1月 10 22:19 io.c 35 -rwxrwxr-x 1 ghostwu ghostwu 8824 1月 10 20:47 strtol 36 -rw-rw-r-- 1 ghostwu ghostwu 616 1月 10 20:47 strtol.c 37 -rw-rw-r-- 1 ghostwu ghostwu 1007 1月 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