開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第7章(入出力)、7.7(行の入出力)、演習7-8を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 7-8.
コード
sample.c
#include <stdio.h> #include <string.h> #define MAXLINE 1000 int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 1, pageno = 1; int c; FILE *fp; char *prog = argv[0]; while (--argc > 0) { if((fp = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "%s can't open %s\n", prog, *argv); exit(1); } if (pageno != 1) printf("\f"); printf("page %d: %s\n", pageno++, *argv); while (fgets(line, MAXLINE, fp) != NULL) { printf("%ld: %s", lineno++, line); } lineno = 0; } }
入出力結果(Terminal)
$ ./a.out $ ./a.out sample.c page 1: sample.c 1: #include <stdio.h> 2: #include <string.h> 3: #define MAXLINE 1000 4: 5: int main(int argc, char *argv[]) 6: { 7: char line[MAXLINE]; 8: long lineno = 1, pageno = 1; 9: int c; 10: FILE *fp; 11: char *prog = argv[0]; 12: 13: while (--argc > 0) { 14: if((fp = fopen(*++argv, "r")) == NULL) { 15: fprintf(stderr, "%s can't open %s\n", prog, *argv); 16: exit(1); 17: } 18: if (pageno != 1) 19: printf("\f"); 20: printf("page %d: %s\n", pageno++, *argv); 21: while (fgets(line, MAXLINE, fp) != NULL) { 22: printf("%ld: %s", lineno++, line); 23: } 24: lineno = 0; 25: } 26: } $ ./a.out sample.c sample.txt page 1: sample.c 1: #include <stdio.h> 2: #include <string.h> 3: #define MAXLINE 1000 4: 5: int main(int argc, char *argv[]) 6: { 7: char line[MAXLINE]; 8: long lineno = 1, pageno = 1; 9: int c; 10: FILE *fp; 11: char *prog = argv[0]; 12: 13: while (--argc > 0) { 14: if((fp = fopen(*++argv, "r")) == NULL) { 15: fprintf(stderr, "%s can't open %s\n", prog, *argv); 16: exit(1); 17: } 18: if (pageno != 1) 19: printf("\f"); 20: printf("page %d: %s\n", pageno++, *argv); 21: while (fgets(line, MAXLINE, fp) != NULL) { 22: printf("%ld: %s", lineno++, line); 23: } 24: lineno = 0; 25: } 26: } page 2: sample.txt 0: Ah Love! could you and I with Fate conspire 1: To grasp this sorry Scheme of Things entire, 2: Would not we shatter it to bits -- and then 3: Re-mould it nearer to the Heart's Desire! 4: ah love! could you and i with fate conspire 5: to grasp this sorry scheme of things entire, 6: would not we shatter it to bits -- and then 7: re-mould it nearer to the heart's desire! 8: !@#$%Ah Love! could you and I with Fate conspire 9: !@#$%To grasp this sorry Scheme of Things entire, 10: !@#$%Would not we shatter it to bits -- and then 11: !@#$%Re-mould it nearer to the Heart's Desire! 12: !@#$%ah love! could you and i with fate conspire 13: !@#$%to grasp this sorry scheme of things entire, 14: !@#$%would not we shatter it to bits -- and then 15: !@#$%re-mould it nearer to the heart's desire! $ ./a.out abcde sample.c ./a.out can't open abcde $ ./a.out sample.c abcde page 1: sample.c 1: #include <stdio.h> 2: #include <string.h> 3: #define MAXLINE 1000 4: 5: int main(int argc, char *argv[]) 6: { 7: char line[MAXLINE]; 8: long lineno = 1, pageno = 1; 9: int c; 10: FILE *fp; 11: char *prog = argv[0]; 12: 13: while (--argc > 0) { 14: if((fp = fopen(*++argv, "r")) == NULL) { 15: fprintf(stderr, "%s can't open %s\n", prog, *argv); 16: exit(1); 17: } 18: if (pageno != 1) 19: printf("\f"); 20: printf("page %d: %s\n", pageno++, *argv); 21: while (fgets(line, MAXLINE, fp) != NULL) { 22: printf("%ld: %s", lineno++, line); 23: } 24: lineno = 0; 25: } 26: } ./a.out can't open abcde $
0 コメント:
コメントを投稿