開発環境
- 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-6を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 7-6.
コード
sample.c
#include <stdio.h> int main(int argc, char *argv[]) { FILE *fp1, *fp2; void fcmp(FILE *, FILE *); if (argc != 3) { fprintf(stderr, "コマンドライン引数に2つのファイルを指定して下さい。\n"); exit(1); } if ((fp1 = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "%sを開けませんでした。\n", *argv); } if ((fp2 = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "%sを開けませんでした。\n", *argv); } fcmp(fp1, fp2); fclose(fp1); fclose(fp2); return 0; } #include <stdlib.h> #include <string.h> #define MAXLINE 1000 void fcmp(FILE *fp1, FILE *fp2) { char line1[MAXLINE], line2[MAXLINE]; char *lp1, *lp2; do { lp1 = fgets(line1, MAXLINE, fp1); lp2 = fgets(line2, MAXLINE, fp2); if (lp1 == line1 && lp2 == line2) { if (strcmp(line1, line2) != 0) { printf("違っている最初の行\n"); printf("%s", line1); printf("%s", line2); lp1 = NULL; } } else if (lp1 == line1 && lp2 != line2) { printf("違っている最初の行(2つ目のファイルは終了)\n"); printf("%s", line1); } else if (lp1 != line1 && lp2 == line2) { printf("違っている最初の行(1つ目のファイルは終了)\n"); printf("%s", line2); } else { printf("違っている行はない。\n"); lp1 = NULL; } } while (lp1 == line1 && lp2 == line2); }
入出力結果(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! 1 5 2 4 3 !@#$%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 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! $ ./a.out sample.txt tmp.txt 違っている最初の行 !@#$%Would not we shatter it to bits -- and then python $ ./a.out tmp.txt sample.txt 違っている最初の行 python !@#$%Would not we shatter it to bits -- and then $ ./a.out tmp.txt tmp.txt 違っている行はない。 $ ./a.out sample.c sample.c 違っている行はない。 $
0 コメント:
コメントを投稿