博客
关于我
一个C/C++ 命令行参数处理的程序
阅读量:374 次
发布时间:2019-03-04

本文共 1539 字,大约阅读时间需要 5 分钟。

?????K&R C??????UNIX????????????????- x?- n??????????- xn?????????????????????????

??????????????????????????????- x?- n??????????????vs2015?gcc??????

???????

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#define MAXLINE 1000int getline_s(char *line, int max);
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]) {            switch (c) {                case 'x':                    except = 1;                    break;                case 'n':                    number = 1;                    break;                default:                    printf("find: illegal optional %c\n", c);                    argc = 0;                    found = -1;                    break;            }        }    }    if (argc != 1) {        printf("Usage: find -x -n pattern\n");    } else {        while (getline_s(line, MAXLINE) > 0) {            lineno++;            if ((strstr(line, *argv) != NULL) != except) {                if (number)                    printf("%ld:", lineno);                printf("%s", line);                found++;            }        }    }    return found;}int getline_s(char *s, int max){    int c;    char *p = s;    while ((c = getchar()) != EOF && c != '\n') {        *s++ = c;    }    if (c == '\n') {        *s++ = c;    }    *s = '\0';    return s - p;}

????????????????

./a.out -x -n test

???????

./a.out test

???????- xn ???

./a.out -xn

转载地址:http://yabg.baihongyu.com/

你可能感兴趣的文章
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>
Python关于strftime函数详细解析 附实战代码
查看>>
Python3 tutorial day3
查看>>
python3 week4
查看>>
Python3 创建数组
查看>>
python3 套接字异常(a byte-like object is required not str)
查看>>
python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
查看>>
python3 文本变图片
查看>>
Python3 查看 com 组件的属性
查看>>
python3 案例分享--发邮件
查看>>
python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
查看>>
Python3 求最大/小值及索引值 Numpy
查看>>
python3+django使用celery执行某些任务失败的解决方案
查看>>
Python3+Requests+Unittest实战系列【一】
查看>>
python3+requests+unittest实战系列【一】
查看>>
python3+requests+unittest实战系列【二】
查看>>
Python3+Requests+Unittest实战系列【二】
查看>>