|
Generated by UTF-8 version of diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. |
../02-01/02-01-OpenFile-NotSoGood.c | 02-02-OpenFile-Full.c | |||
---|---|---|---|---|
35 lines 892 bytes Last modified : Sun Dec 29 01:15:32 2013 |
47 lines 1275 bytes Last modified : Thu Dec 26 06:50:51 2013 |
|||
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 | // 計算機序論2・実習 (筑波大学工学システム学類) | 4 | // 計算機序論2・実習 (筑波大学工学システム学類) | |
5 | 5 | |||
6 | // 2013/12/26a kameda[at]iit.tsukuba.ac.jp | |||
7 | // 2013/12/24a kameda[at]iit.tsukuba.ac.jp | 6 | // 2013/12/24a kameda[at]iit.tsukuba.ac.jp | |
8 | // 2011/11/22a kameda[at]iit.tsukuba.ac.jp | 7 | // 2011/11/22a kameda[at]iit.tsukuba.ac.jp | |
9 | // 02.01. ファイルのぞんざいな開き方 | 8 | // 02.02. ファイルの上品な開き方 | |
10 | 9 | |||
11 | #include <stdio.h> | 10 | #include <stdio.h> | |
12 | 11 | |||
13 | int main(int argc, char *argv[]){ | 12 | int main(int argc, char *argv[]){ | |
14 | FILE *filetoopen; | 13 | FILE *filetoopen = NULL; // A pointer to FILE structure | |
15 | 14 | |||
16 | // Show number of options at command line | 15 | // Show number of options at command line | |
17 | printf("Number of options : %d\n", argc); | 16 | printf("Number of Options : %d\n", argc); | |
17 | ||||
18 | // We need at least one option for a file name to open | |||
19 | if (argc <= 1) { // What happens if you put other figure here? | |||
20 | printf("Error: You need to specify a file to open.\n"); | |||
21 | return 1; | |||
22 | } | |||
18 | 23 | |||
19 | // Try to open it | 24 | // Try to open it | |
20 | printf("Let's open \"%s\"! \n", argv[1]); | 25 | printf("Let's open \"%s\"! \n", argv[1]); | |
21 | filetoopen = fopen(argv[1], "r"); | 26 | filetoopen = fopen(argv[1], "r"); | |
27 | if (filetoopen == NULL) { | |||
28 | printf("Error: Failed to open/read \"%s\".\n", argv[1]); | |||
29 | return 2; | |||
30 | } | |||
22 | printf("Ready to read \"%s\"\n", argv[1]); | 31 | printf("Ready to read \"%s\"\n", argv[1]); | |
23 | 32 | |||
24 | // Do something to get data from the file ... | 33 | // Do something to get data from the file ... | |
25 | 34 | |||
26 | // And close it | 35 | // And close it | |
27 | fclose(filetoopen); | 36 | if (fclose(filetoopen) != 0) { | |
37 | printf("Error: Failed to close \"%s\".\n", argv[1]); | |||
38 | return 3; | |||
39 | } | |||
28 | printf("File %s is now closed.\n", argv[1]); | 40 | printf("File %s is now closed.\n", argv[1]); | |
29 | 41 | |||
30 | // Do other things ... | 42 | // Do other things ... | |
31 | 43 | |||
32 | printf("All set. Bye!\n"); | 44 | printf("All set. Bye!\n"); | |
33 | return 0; | 45 | return 0; | |
34 | } | 46 | } | |
35 | 47 |