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

本文介绍了一个模仿Linux中strings命令的程序实现。该程序可以从文件中提取连续4个以上的可打印字符,并支持从标准输入读取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本程序实现从文件中提取连续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.\n",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=='\t')&&last < BUFSIZE-1)//标准strings命令也接受'\t'
     buf[last++] = c;
     else 
     {
       if (last >= 4) 
        {
          buf[last] = '\0';
          printf("%s\n", buf);
         }
       last = 0;
     }
     if (c == EOF) break;
    }
}









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

转载于:https://www.cnblogs.com/luo-peng/p/4646258.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值