|
Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. |
../102-2/102-2-ReadFile-Full.c | 109-2-OpenFile.c | |||
---|---|---|---|---|
41 lines 1054 bytes Last modified : Mon Nov 7 06:00:41 2011 |
56 lines 1369 bytes Last modified : Mon Nov 7 11:17:57 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 | // 2.2 ファイルを指定してのオープン(エラー対策済) | 6 | // 9.2 指定ファイルのオープン | |
7 | 7 | |||
8 | #include <stdio.h> | 8 | #include <stdio.h> | |
9 | 9 | |||
10 | int main(int argc, char *argv[]){ | 10 | // +---------------------------------------------------- | |
11 | // モデルのファイルからの読込 | |||
12 | // +---------------------------------------------------- | |||
13 | int ic2_ReadModel(char *filename) { | |||
11 | FILE *filetoopen = NULL; // A pointer to FILE structure | 14 | FILE *filetoopen = NULL; // A pointer to FILE structure | |
12 | 15 | |||
13 | // Show number of options at command line | |||
14 | printf("Number of Options : %d\n", argc); | |||
15 | ||||
16 | // We need at least one option to indicate a file to open | 16 | // We need at least one option to indicate a file to open | |
17 | if (argc <= 1) { // What happens if you put other figure here? | 17 | if (filename == NULL) { | |
18 | printf("Error: You need to specify a file to open.\n"); | 18 | printf("Error: You need to specify a one file to open.\n"); | |
19 | return -1; | 19 | return -1; | |
20 | } | 20 | } | |
21 | 21 | |||
22 | // Try to open it | 22 | // Try to open it | |
23 | filetoopen = fopen(argv[1], "r"); | 23 | filetoopen = fopen(filename, "r"); | |
24 | if (filetoopen == NULL) { | 24 | if (filetoopen == NULL) { | |
25 | printf("Error: Failed to open/read \"%s\".\n", argv[1]); | 25 | printf("Error: Failed to open/read \"%s\".\n", filename); | |
26 | return -2; | 26 | return -2; | |
27 | } | 27 | } | |
28 | printf("Ready to read \"%s\"\n", argv[1]); | 28 | printf("Reading model from \"%s\"\n", filename); | |
29 | 29 | |||
30 | // Do something ... | 30 | // Do something ... | |
31 | 31 | |||
32 | // And close it | 32 | // And close it | |
33 | if (fclose(filetoopen) != 0) { | 33 | if (fclose(filetoopen) != 0) { | |
34 | printf("Error: Failed to close \"%s\".\n", argv[1]); | 34 | printf("Error: Failed to close \"%s\".\n", filename); | |
35 | return -3; | 35 | // error, but we get data anyway... | |
36 | } | |||
37 | ||||
38 | printf("Finish reading the model.\n"); | |||
39 | return 0; | |||
40 | } | |||
41 | ||||
42 | int main(int argc, char *argv[]){ | |||
43 | ||||
44 | if (argc <= 1) { | |||
45 | printf("Error: no model file is specified.\n"); | |||
46 | return 1; | |||
47 | } | |||
48 | ||||
49 | if (ic2_ReadModel(argv[1]) < 0) { | |||
50 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | |||
51 | return 1; | |||
36 | } | 52 | } | |
37 | 53 | |||
38 | printf("All set. Bye!\n"); | |||
39 | return 0; | 54 | return 0; | |
40 | } | 55 | } | |
41 | 56 |