c语言调用grep函数如何写,用C语言编写一个grep功能的程序

匿名用户

1级

2015-08-31 回答

/* 一组宏定义,不过此函数没有用 */

#define ESIZE 1024

#define INIT register unsigned char *sp=instring;

#define GETC() (*sp++)

#define PEEKC() (*sp)

#define UNGETC(c) (--sp)

#define RETURN(c) return c;

#define ERROR(c) { regerr(c); return(NULL); }

#include "stdio.h"

#include "string.h"

/* 这是什么头文件?? */

#include "regexp.h"

/* 此文件内定义的全局变量 */

int i;

unsigned char *nextpos;

static unsigned char lbuf[512], ebuf[ESIZE];

FILE *fp;

/* 函数声明 */

int regerr();

/* 可接受命令行参数 */

int main(int argc,char *argv[])

{

if(argc < 3) /* 输入的参数不足三个,出错 */

{

/* 向标准出错设备输出信息 */

fprintf(stderr,"Use: %s regular_expr files ..\n",argv[0]);

exit(-1); /* 退出程序 */

}

/* 没见过compile函数 */

nextpos = compile(argv[1], ebuf, &ebuf[ESIZE],'\0');

if(nextpos)

for(i=2;i

{

if((fp=fopen(argv[i],"rb"))==NULL)

printf("%s: read failure.\n",argv[i]);

else

/* 没见过step函数 */

while(fgets(lbuf,sizeof(lbuf),fp))

if(step(lbuf,ebuf)) printf("%s: %s",argv[i],lbuf);

fclose(fp); /* 关闭文件 */

}

}

/* 向标准出错设备输出信息,即屏幕 */

int regerr(int c) /* 函数不完整,返回值没用 */

{

fprintf(stderr,"Error %d.\n", c);

}

grep的功能

grep从一个或多个文本文件中查找符合指定范本(正则表达式)的行,并把查找结果输出到指定设备(默认到屏幕)。

正则表达式?还要模式匹配?有点小复杂啊。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值