zoukankan      html  css  js  c++  java
  • linux系统编程:read,write与lseek的综合应用

    这个实例根据命令行参数进行相应的读学操作:

    用法:

    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>}...
    ", 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( "字符串->整形转换失败
    " );
     55                     exit( -1 );
     56                 }
     57                 lseek( fd, res, SEEK_CUR );    
     58                 printf( "%s--->指针移动成功
    ", 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写入失败
    ", argv[i] );
     65                 }
     66                 printf( "%s成功写入%ld个字节
    ", 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( "字符串->整形转换失败
    " );
     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( "数据读取失败
    " );
     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]中的一个开头
    ", 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 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 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--->指针移动成功 43 r7: ghostwughostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 R7 44 s1000--->指针移动成功
  • 相关阅读:
    Ubunt下的MySql设置允许远程连接
    C# 子窗体最大化后Icon与MDI窗体的菜单合拼的问题
    Android Studio安装、配置、第一个程序的那些坑
    LeetCode刷题系列——Add Two Numbers
    比特币和区块链的初探
    《Master Bitcoin》学习笔记02——比特币的交易模型
    《Master Bitcoin》学习笔记01
    jQuery 追加元素的方法如append、prepend、before、after
    获取当前月第一天和最后一天
    动态添加json数据
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8261151.html
Copyright © 2011-2022 走看看