開発環境
- 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-6を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 7-6.
コード
sample.c
#include <stdio.h> #include <string.h> #define MAXLINE 1000 int main(int argc, char *argv[]) { FILE *fp1, *fp2; char line1[MAXLINE], line2[MAXLINE]; char *lp1, lp2; if (argc != 3) { fprintf(stderr, "コマンドライン引数に2つのファイルが指定されてない\n"); exit(1); } else if ((fp1 = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "一つ目のファイル %s を開くの失敗\n", *argv); exit(1); } else if ((fp2 = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "二つ目のファイル %s を開くの失敗\n", *argv); exit(1); } else { do { lp1 = fgets(line1, MAXLINE, fp1); lp2 = fgets(line2, MAXLINE, fp2); } while (lp1 != NULL && lp2 != NULL && (strcmp(line1, line2) == 0)); if (lp1 == NULL && lp2 != NULL) printf("line1: ファイル終了\nline2: %s", line2); else if (lp1 != NULL && lp2 == NULL) printf("line1: %sline2: ファイル終了\n", line1); else if (lp1 == NULL && lp2 == NULL) printf("違っている行は無い\n"); else printf("line1 %sline2 %s", line1, line2); } return 0; }
入出力結果(Terminal)
$ cat sample.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! !@#$%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! $ cp sample.txt sample1.txt $ cat sample2.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! 1234567890 !@#$%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! $ cat sample3.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! $ ./a.out sample.txt sample1.txt 違っている行は無い $ ./a.out sample.txt sample2.txt line1 !@#$%Ah Love! could you and I with Fate conspire line2 1234567890 $ ./a.out sample.txt sample3.txt line1: !@#$%Ah Love! could you and I with Fate conspire line2: ファイル終了 $ ./a.out sample1.txt sample.txt 違っている行は無い $ ./a.out sample2.txt sample.txt line1 1234567890 line2 !@#$%Ah Love! could you and I with Fate conspire $ ./a.out sample3.txt sample.txt line1: ファイル終了 line2: !@#$%Ah Love! could you and I with Fate conspire $
0 コメント:
コメントを投稿