- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
Latest commit
103 lines (82 loc) · 2.41 KB
/
Copy pathtest.cpp
File metadata and controls
103 lines (82 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stdexcept>
#include<format>
#defineVERSION"3.2.0"
#if !defined(BUILD_NUMBER)
#error BUILD_NUMBER must be defined when compiling this source file
#endif
// a structure carrying command line option values
structoptions_t {
bool print_help = false;
bool print_version = false;
};
voidprint_version(void)
{
printf("\nv%s build %d\n", VERSION, BUILD_NUMBER);
}
voidprint_help(void)
{
printf("Usage: test [options]\n\n");
printf(" -h, --help Show this help message\n");
printf(" -v, --version Show version information\n");
}
options_tparse_options(int argc, char *argv[])
{
options_t options;
for(int i = 1; i < argc; i++) {
if(*argv[i] != '-')
throwstd::runtime_error(std::format("Unknown argument {:s}", argv[i]));
switch (argv[i][1]) {
case'h':
options.print_help = true;
break;
case'v':
options.print_version = true;
break;
case'-':
if(!strcmp(argv[i]+2, "help"))
options.print_help = true;
elseif (!strcmp(argv[i]+2, "version"))
options.print_version = true;
else
throwstd::runtime_error(std::format("Unknown long option {:s}", argv[i]));
break;
default:
throwstd::runtime_error(std::format("Unknown option {:s}", argv[i]));
}
}
return options;
}
intmain(int argc, char *argv[])
{
try {
options_t options = parse_options(argc, argv);
if(options.print_version) {
print_version();
returnEXIT_SUCCESS;
}
if(options.print_help) {
print_help();
returnEXIT_SUCCESS;
}
FILE *csv = fopen("csv/20221211/abc.csv", "r");
if(!csv)
throwstd::runtime_error("Cannot open CSV");
char buffer[256];
while(fgets(buffer, sizeof(buffer), csv)) {
printf("%s", buffer);
}
if(ferror(csv)) {
fclose(csv);
throwstd::runtime_error("Error reading CSV");
}
fclose(csv);
returnEXIT_SUCCESS;
}
catch (const std::exception& error) {
fprintf(stderr, "%s\n", error.what());
}
returnEXIT_FAILURE;
}