C语言-C语言程序设计-Function-Find

C语言-C语言程序设计-Function-Find

照着书敲了一遍然后又重新读了一次才发现程序通过while循环识别的 -xn这种输入。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line, int max);
/* find函数: 打印所有与第一个参数指定的模式相匹配的行*/
/* find -x -n */
int main(int argc, char *argv[])
{
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0,found = 0;
while(--argc > 0 && (*++argv)[0] == '-')
while( c = *++argv[0]) //这里循环直接识别了 -xn
switch(c){
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
if(argc != 1)
printf("Usage:find -x -n pattern\n");
else
while(getline(line, MAXLINE) > 0){
lineno++;
if((strstr(line, *argv) != NULL) != except){
if(number)
printf("%ld", line);
printf("%s", line);
found++;
}
}
return found;
}
int getline(char *line, int max)
{
if(fgets(line, max, stdin) == NULL)
return 0;
else
return strlen(line);
}
posted @   Theseus‘Ship  阅读(213)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
Live2D
欢迎阅读『C语言-C语言程序设计-Function-Find』
点击右上角即可分享
微信分享提示