開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第7章(入出力)、7.5(ファイル・アクセス)、7.6(エラー処理 - Stderr と Exit)、7.7(行の入出力)、演習7-7を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 7-7.
コード
sample.c
#include <stdio.h> #include <string.h> #define MAXLINE 1000 int main(int argc, char *argv[]) { char line[MAXLINE], pattern[MAXLINE]; int c, except = 0, number = 0; FILE *fp; void find(FILE *, int, int, char *, char *); 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 option %c\n", c); argc = 0; break; } } } if (argc >= 1){ strcpy(pattern, *argv); } else { printf("Usage: find [-x] [-n] pattern [file …]\n"); exit(1); } if (argc == 1) { find(stdin, except, number, pattern, ""); } else { while (--argc > 0) { if ((fp = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "find: can't open %s\n", *argv); exit(1); } else { find(fp, except, number, pattern, *argv); fclose(fp); } } } return 0; } void find(FILE *fp, int except, int number, char *pattern, char *fname) { char line[MAXLINE]; long lineno = 0; while (fgets(line, MAXLINE, fp) != NULL) { lineno++; if ((strstr(line, pattern) != NULL) != except) { if (*fname) { printf("ファイル名: %s\n", fname); } if (number) { printf("%ld: ", lineno); } printf("%s", line); } } }
入出力結果(Terminal)
$ ./a.out ab aabaa aabaa aaaaa $ ./a.out -n ab aabaa 1: aabaa bbbbb aabaa 3: aabaa aaaaa $ ./a.out -x ab aabaa bbbbb bbbbb $ ./a.out -n case sample.c ファイル名: sample.c 16: case 'x': ファイル名: sample.c 19: case 'n': $ ./a.out -nx " " sample.c ファイル名: sample.c 3: ファイル名: sample.c 5: ファイル名: sample.c 7: { ファイル名: sample.c 49: } ファイル名: sample.c 50: ファイル名: sample.c 52: { ファイル名: sample.c 68: }$ ./a.out -nx " " sample.c ファイル名: sample.c 3: ファイル名: sample.c 5: ファイル名: sample.c 7: { ファイル名: sample.c 49: } ファイル名: sample.c 50: ファイル名: sample.c 52: { ファイル名: sample.c 68: } $ ./a.out -nx " " sample.c tmp.txt ファイル名: sample.c 3: ファイル名: sample.c 5: ファイル名: sample.c 7: { ファイル名: sample.c 49: } ファイル名: sample.c 50: ファイル名: sample.c 52: { ファイル名: sample.c 68: } ファイル名: tmp.txt 9: 1 ファイル名: tmp.txt 10: 5 ファイル名: tmp.txt 11: 2 ファイル名: tmp.txt 12: 4 ファイル名: tmp.txt 13: 3 ファイル名: tmp.txt 16: python $ cat tmp.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! ah love! could you and i with fate conspire to grasp this sorry scheme of things entire, would not we shatter it to bits -- and then re-mould it nearer to the heart's desire! 1 5 2 4 3 !@#$%Ah Love! could you and I with Fate conspire !@#$%To grasp this sorry Scheme of Things entire, python !@#$%Would not we shatter it to bits -- and then !@#$%Re-mould it nearer to the Heart's Desire! !@#$%ah love! could you and i with fate conspire !@#$%to grasp this sorry scheme of things entire, !@#$%would not we shatter it to bits -- and then !@#$%re-mould it nearer to the heart's desire! $
0 コメント:
コメントを投稿