| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../109-2/109-2-OpenFile.c | 109-3-ReadLine.c | |||
|---|---|---|---|---|
| 56 lines 1369 bytes Last modified : Mon Nov 7 11:17:57 2011 | 78 lines 2197 bytes Last modified : Mon Nov 7 11:16:16 2011 | |||
| 1 | // Keisanki Joron 2 (Introduction to Computing II) | 1 | // Keisanki Joron 2 (Introduction to Computing II) | |
| 2 | // Dept. of Engineering Systems, University of Tsukuba | 2 | // Dept. of Engineering Systems, University of Tsukuba | |
| 3 | // [UTF-8 / Unix] | 3 | // [UTF-8 / Unix] | |
| 4 | 4 | |||
| 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | |
| 6 | // 9.2 指定ファイルのオープン | 6 | // 9.3 行毎の読み込み | |
| 7 | 7 | |||
| 8 | #include <stdio.h> | 8 | #include <stdio.h> | |
| 9 | 9 | |||
| 10 | // +---------------------------------------------------- | 10 | // +---------------------------------------------------- | |
| 11 | // モデルのファイルからの読込 | 11 | // モデルのファイルからの読込 | |
| 12 | // +---------------------------------------------------- | 12 | // +---------------------------------------------------- | |
| 13 | int ic2_ReadModel(char *filename) { | 13 | int ic2_ReadModel(char *filename) { | |
| 14 | FILE *filetoopen = NULL; // A pointer to FILE structure | 14 | FILE *filetoopen = NULL; // A pointer to FILE structure | |
| 15 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |||
| 16 | char firstword[256]; // コメント行かどうかの判定用 | |||
| 17 | int linenumber = 0; // ファイル中の行番号 | |||
| 18 | int patchnumber = 0; // パッチ数 | |||
| 15 | 19 | |||
| 16 | // We need at least one option to indicate a file to open | 20 | // We need at least one option to indicate a file to open | |
| 17 | if (filename == NULL) { | 21 | if (filename == NULL) { | |
| 18 | printf("Error: You need to specify a one file to open.\n"); | 22 | printf("Error: You need to specify a one file to open.\n"); | |
| 19 | return -1; | 23 | return -1; | |
| 20 | } | 24 | } | |
| 21 | 25 | |||
| 22 | // Try to open it | 26 | // Try to open it | |
| 23 | filetoopen = fopen(filename, "r"); | 27 | filetoopen = fopen(filename, "r"); | |
| 24 | if (filetoopen == NULL) { | 28 | if (filetoopen == NULL) { | |
| 25 | printf("Error: Failed to open/read \"%s\".\n", filename); | 29 | printf("Error: Failed to open/read \"%s\".\n", filename); | |
| 26 | return -2; | 30 | return -2; | |
| 27 | } | 31 | } | |
| 28 | printf("Reading model from \"%s\"\n", filename); | 32 | printf("Reading model from \"%s\"\n", filename); | |
| 29 | 33 | |||
| 30 | // Do something ... | 34 | // 1行ずつ読込 | |
| 35 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | |||
| 36 | linenumber++; | |||
| 37 | ||||
| 38 | // もし行内に1文字もなければ(1単語もなければ)次行へ | |||
| 39 | if (sscanf(oneline, "%256s", firstword) < 1) | |||
| 40 | continue; | |||
| 41 | // もし先頭が#で始まっていれば次行へ | |||
| 42 | if (firstword[0] == '#') | |||
| 43 | continue; | |||
| 44 | // 他のエラートラップ | |||
| 45 | if (0) { | |||
| 46 | printf("Skip(line=%d): %s\n", linenumber, oneline); | |||
| 47 | continue; | |||
| 48 | } | |||
| 49 | ||||
| 50 | printf("Valid: %d: \"%s\"\n", linenumber, oneline); | |||
| 51 | patchnumber++; | |||
| 52 | } | |||
| 31 | 53 | |||
| 32 | // And close it | 54 | // And close it | |
| 33 | if (fclose(filetoopen) != 0) { | 55 | if (fclose(filetoopen) != 0) { | |
| 34 | printf("Error: Failed to close \"%s\".\n", filename); | 56 | printf("Error: Failed to close \"%s\".\n", filename); | |
| 35 | // error, but we get data anyway... | 57 | // error, but we get data anyway... | |
| 36 | } | 58 | } | |
| 37 | 59 | |||
| 38 | printf("Finish reading the model.\n"); | 60 | printf("Finish reading the model (%d patches).\n", patchnumber); | |
| 39 | return 0; | 61 | return patchnumber; | |
| 40 | } | 62 | } | |
| 41 | 63 | |||
| 42 | int main(int argc, char *argv[]){ | 64 | int main(int argc, char *argv[]){ | |
| 43 | 65 | |||
| 44 | if (argc <= 1) { | 66 | if (argc <= 1) { | |
| 45 | printf("Error: no model file is specified.\n"); | 67 | printf("Error: no model file is specified.\n"); | |
| 46 | return 1; | 68 | return 1; | |
| 47 | } | 69 | } | |
| 48 | 70 | |||
| 49 | if (ic2_ReadModel(argv[1]) < 0) { | 71 | if (ic2_ReadModel(argv[1]) < 0) { | |
| 50 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | 72 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | |
| 51 | return 1; | 73 | return 1; | |
| 52 | } | 74 | } | |
| 53 | 75 | |||
| 54 | return 0; | 76 | return 0; | |
| 55 | } | 77 | } | |
| 56 | 78 |