博客
关于我
一个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/

你可能感兴趣的文章
Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>
python | 高效使用Python工具自动生成模块文档的秘诀
查看>>
python 一个list去除另一个list中的值
查看>>
python 三大框架的 介绍。
查看>>
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python读取word表格内容(1)
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
python 中PIL.Image和OpenCV图像格式相互转换
查看>>
Python 中Semaphore 信号量对象、Event事件、Condition
查看>>
python 中with的使用及样例
查看>>
python读取wav文件并播放[pyaudio/wave]
查看>>
python读取txt文件的行数
查看>>
Python 中内置的最大堆 API
查看>>
Python 中只有一个 True 和一个 False 对象吗?
查看>>
python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
查看>>
Python 中多线程与多处理之间的区别
查看>>