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

你可能感兴趣的文章
PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别 应用场景分析
查看>>
Postgresql CopyManager 流式批量数据入库
查看>>
PostgreSQL cube 插件 - 多维空间对象
查看>>
PostgreSQL Daily Maintenance - cluster table
查看>>
PostgreSQL on Linux 最佳部署手册
查看>>
PostgreSQL Oracle 兼容性之 - pipelined
查看>>
PostgreSQL Point-In-Time Recovery (Incremental Backup)
查看>>
postgresql Streaming Replication监控与注意事项
查看>>
postgresql 不需要付费_使用数据传输在PostgreSQL执行 外部连接运算符
查看>>
postgresql 主从配置_生产环境postgresql主从环境配置
查看>>
postgresql 函数&存储过程 ; 递归查询
查看>>
PostgreSQL 分组聚合查询中 filter 子句替换 case when
查看>>
PostgreSQL 同步流复制锁瓶颈分析
查看>>
PostgreSQL 备份与还原命令 pg_dump
查看>>
Postgresql 外部表插件postgres_fdw的安装和使用
查看>>
PostgreSQL 如何从崩溃状态恢复(上)
查看>>
PostgreSQL 存储过程基本语法
查看>>
PostgreSQL 实现批量更新、删除、插入
查看>>
PostgreSQL 导入 .gz 备份文件
查看>>
PostgreSQL 批量插入&更新数据时报错(ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time)
查看>>