• strings命令的实现 2014-06-02 00:17 355人阅读 评论(0) 收藏


    本程序实现从文件中提取连续4个以上的可打印字符。模仿linux中string命令

    #include <stdio.h>
    #include<stdlib.h>
    #include <ctype.h>
    #define BUFSIZE 4096
    void strings(FILE*fp);
    int main(int argc,char*argv[])
    {
        FILE* fp;
        if(argc==1)
        {
          fp=stdin;
          strings(fp);
        }  
        else
        {
          int i=1;
          for(;i<argc;i++)
          {
            if((fp=fopen(argv[i],"r"))==NULL)
            {
              fprintf(stderr,"open %s error.
    ",argv[i]);
              continue;
            }
            strings(fp);
            close(fp);
          }
        }
        return 0;
    }
    
    void strings(FILE*fp)
    { 
       int c;
       char buf[BUFSIZE];
       int last = 0;
       while(1)
       {
         c = getc(fp);
         if ((isprint(c)||c=='	')&&last < BUFSIZE-1)//标准strings命令也接受'	'
         buf[last++] = c;
         else 
         {
           if (last >= 4) 
            {
              buf[last] = '';
              printf("%s
    ", buf);
             }
           last = 0;
         }
         if (c == EOF) break;
        }
    }
    
    
    
    
    
    
    
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    vue 插件的使用 todolist案例
    vue 传值 混入mixin
    vue 生命周期函数
    vue 指令总结
    vue 其它的指令
    vue 监听数据变化的原理 表单数据的收集
    vue for循环中的key
    vue 学习
    vue 学习
    HDU 1029
  • 原文地址:https://www.cnblogs.com/luo-peng/p/4646258.html
Copyright © 2020-2023  润新知