Commit 001f30e5ca9183b2901ce1cd7683e739ebd31c14
1 parent
64179f09
version recup_fichier qui fonctionne
Showing
1 changed file
with
131 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,131 @@ | @@ -0,0 +1,131 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include <stdbool.h> | ||
4 | +#include <stdlib.h> | ||
5 | + | ||
6 | +// Conditionals | ||
7 | +const bool IS_DEBUG = true; | ||
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 | + char *string; //pointeur de string va être utiliser dans strsep | ||
67 | + | ||
68 | + | ||
69 | + // Check if the file is really opened | ||
70 | + if (NULL == fp) | ||
71 | + { | ||
72 | + fprintf(stderr, "Unable to open file: %s\n", filename); | ||
73 | + return; | ||
74 | + } | ||
75 | + | ||
76 | + // 1st row is a header with field descriptions | ||
77 | + fgets(buffer, BUFFER_SIZE, fp); | ||
78 | + read_csv_header(buffer); | ||
79 | + | ||
80 | + string = strdup(buffer); //string pointe vers la chaine de caractère buffer | ||
81 | + | ||
82 | + // Remaining rows are the entries | ||
83 | + while ( NULL != fgets(buffer, BUFFER_SIZE, fp) ) | ||
84 | + { | ||
85 | + char* token; | ||
86 | + unsigned int i = 0; | ||
87 | + | ||
88 | + string = strdup(buffer); //string pointe vers la chaine de caractère buffer | ||
89 | + | ||
90 | + | ||
91 | + // strsep init. | ||
92 | + token = strsep(&string, CSV_DELIMITERS); | ||
93 | + //strsep permet de séparer une chaine de caractère en prenant en compte les ,, | ||
94 | + | ||
95 | + while (NULL != token) | ||
96 | + { | ||
97 | + if (IS_DEBUG) printf("Field %d is %s\n", i++, token); | ||
98 | + | ||
99 | + // ... | ||
100 | + // you can strcpy the `token` string in your data structures | ||
101 | + // ... | ||
102 | + | ||
103 | + token = strsep(&string, CSV_DELIMITERS); | ||
104 | + } | ||
105 | + } | ||
106 | + | ||
107 | + fclose(fp); | ||
108 | +} | ||
109 | + | ||
110 | +void usage(const char * prog_name) | ||
111 | +{ | ||
112 | + printf("Usage is %s your_csv_file\n\n", prog_name); | ||
113 | +} | ||
114 | + | ||
115 | +int main(int argc, char * argv[]) | ||
116 | +{ | ||
117 | + if (2 != argc) | ||
118 | + { | ||
119 | + usage(argv[0]); | ||
120 | + return 0; | ||
121 | + } | ||
122 | + | ||
123 | + read_csv_file(argv[1]); | ||
124 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | ||
125 | + { | ||
126 | + free(CSV_HEADER_FIELDS[i]); | ||
127 | + } | ||
128 | + free(CSV_HEADER_FIELDS); | ||
129 | + | ||
130 | + return 0; | ||
131 | +} | ||
0 | \ No newline at end of file | 132 | \ No newline at end of file |