Commit 296e73d0ec64feb6eba12b61d3fedf7a44810242
1 parent
687539f6
✚ fichier exemple pour lire un CSV
Showing
1 changed file
with
117 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,117 @@ | @@ -0,0 +1,117 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include <stdbool.h> | ||
4 | +#include <stdlib.h> | ||
5 | + | ||
6 | +// Conditionals | ||
7 | +const bool IS_DEBUG = false; | ||
8 | + | ||
9 | +// Constants | ||
10 | +const unsigned int BUFFER_SIZE = 2048; | ||
11 | +const unsigned int FIELD_SIZE = 20; | ||
12 | +const char CSV_DELIMITERS[] = ","; | ||
13 | + | ||
14 | +// Globals | ||
15 | +char** CSV_HEADER_FIELDS; | ||
16 | +unsigned int CSV_NB_FIELDS; | ||
17 | + | ||
18 | +void display_header() | ||
19 | +{ | ||
20 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | ||
21 | + { | ||
22 | + printf("%d - %s\n", i, CSV_HEADER_FIELDS[i]); | ||
23 | + } | ||
24 | +} | ||
25 | + | ||
26 | +void read_csv_header(char * header_line) | ||
27 | +{ | ||
28 | + int line_length = strlen(header_line); | ||
29 | + int nb_fields = 0; | ||
30 | + char* string_ptr = header_line; | ||
31 | + | ||
32 | + // Count the occurrences of delimiters | ||
33 | + while (NULL != string_ptr) | ||
34 | + { | ||
35 | + nb_fields++; | ||
36 | + string_ptr = strpbrk(string_ptr, CSV_DELIMITERS); | ||
37 | + if (NULL != string_ptr) | ||
38 | + { | ||
39 | + string_ptr++; | ||
40 | + } | ||
41 | + } | ||
42 | + | ||
43 | + // Globals allocation | ||
44 | + CSV_NB_FIELDS = nb_fields; | ||
45 | + CSV_HEADER_FIELDS = malloc( nb_fields * sizeof(char*) ); | ||
46 | + | ||
47 | + char* token = strtok(header_line, CSV_DELIMITERS); // strtok init. | ||
48 | + | ||
49 | + // Re-read the line to get the header of the columns | ||
50 | + for (unsigned int i = 0; i < nb_fields; i++) | ||
51 | + { | ||
52 | + CSV_HEADER_FIELDS[i] = malloc( FIELD_SIZE * sizeof(char) ); // alloc | ||
53 | + memset(CSV_HEADER_FIELDS[i], 0, FIELD_SIZE); // 0 init. | ||
54 | + strcpy(CSV_HEADER_FIELDS[i], token); // copy field in the structure | ||
55 | + token = strtok(NULL, CSV_DELIMITERS); // loop to get a new field label | ||
56 | + } | ||
57 | + | ||
58 | + if (IS_DEBUG) display_header(); | ||
59 | +} | ||
60 | + | ||
61 | +void read_csv_file(const char * filename) | ||
62 | +{ | ||
63 | + FILE* fp = fopen(filename, "r"); | ||
64 | + char buffer[BUFFER_SIZE]; | ||
65 | + | ||
66 | + // Check if the file is really opened | ||
67 | + if (NULL == fp) | ||
68 | + { | ||
69 | + fprintf(stderr, "Unable to open file: %s\n", filename); | ||
70 | + return; | ||
71 | + } | ||
72 | + | ||
73 | + // 1st row is a header with field descriptions | ||
74 | + fgets(buffer, BUFFER_SIZE, fp); | ||
75 | + read_csv_header(buffer); | ||
76 | + | ||
77 | + // Remaining rows are the entries | ||
78 | + while ( NULL != fgets(buffer, BUFFER_SIZE, fp) ) | ||
79 | + { | ||
80 | + char* token; | ||
81 | + unsigned int i = 0; | ||
82 | + | ||
83 | + // strtok init. | ||
84 | + token = strtok(buffer, CSV_DELIMITERS); | ||
85 | + | ||
86 | + while (NULL != token) | ||
87 | + { | ||
88 | + if (IS_DEBUG) printf("Field %d is %s\n", i++, token); // you can strcpy the `token` string in your data structures | ||
89 | + token = strtok(NULL, CSV_DELIMITERS); | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + fclose(fp); | ||
94 | +} | ||
95 | + | ||
96 | +void usage(const char * prog_name) | ||
97 | +{ | ||
98 | + printf("Usage is %s your_csv_file\n\n", prog_name); | ||
99 | +} | ||
100 | + | ||
101 | +int main(int argc, char * argv[]) | ||
102 | +{ | ||
103 | + if (2 != argc) | ||
104 | + { | ||
105 | + usage(argv[0]); | ||
106 | + return 0; | ||
107 | + } | ||
108 | + | ||
109 | + read_csv_file(argv[1]); | ||
110 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | ||
111 | + { | ||
112 | + free(CSV_HEADER_FIELDS[i]); | ||
113 | + } | ||
114 | + free(CSV_HEADER_FIELDS); | ||
115 | + | ||
116 | + return 0; | ||
117 | +} | ||
0 | \ No newline at end of file | 118 | \ No newline at end of file |