- 论坛徽章:
- 0
|
楼上可以给我解释一下正则么?
我尝试写了mygrep.c
代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#define maxsize 1024
main(int argc,char *argv[])
{
int fd;
char* target;
char* line;
char* temp;
char buff[maxsize];
int offset=0;
int len1,len2;
int linenum=1;
int flag=0;
if (argc =! 3) {
fprintf(stderr,"Use: %s regular_expr files ..\n", argv[0]);
exit(-1);
}
*target=argv[1];
len1=strlen(target);
target[len1]='\0';
fd=open(argv[2],O_RDONLY);
if(fd==-1)
{
printf("file %s cannot open",argv[2]);
exit(1);
}
int readbyte;
while(readbyte=read(fd,buff,maxsize))
{
if(readbyte==-1) break;
else if(readbyte>0){
char *end;
end[0]='\n';
end[1]='\0';
if(strstr(buff,end)){
int i=0;
for(i=0;i<maxsize&&buff[i]!='\n';i++){
line[i]=buff[i];
}
line[i+1]='\0';
temp=line;
char* p;
while(p=strstr(temp,target)){
flag++;
printf("%d%s",linenum,line);
p+=len1;
temp=p;
}
linenum++;
offset+=i+1;
lseek(fd,offset,SEEK_SET);
}
}
}
}
cygwin编译有以下warning
24:warning:assighnment makes integer from pointer without a cast
25: warning:incompatible implicit declaration of built-in function 'strlen'
41: warining: incompatible implicit declaration of built-in funcion 'strstr'
尝试运行后“command not found”
能不能请各位大虾给我纠一下虫子。。。 |
|