Commit 997055b1f7dc77502141479cb225e9f875b16929

Authored by mdupre1
1 parent 001f30e5

recuperation fichier qvec remplissage du tableau de char

Showing 1 changed file with 134 additions and 0 deletions   Show diff stats
src/recup_fichierV3.c 0 → 100644
... ... @@ -0,0 +1,134 @@
  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 + char* tab_char[50];
  88 +
  89 + string = strdup(buffer); //string pointe vers la chaine de caractère buffer
  90 +
  91 +
  92 + // strsep init.
  93 + token = strsep(&string, CSV_DELIMITERS);
  94 + //strsep permet de séparer une chaine de caractère en prenant en compte les ,,
  95 +
  96 + while (NULL != token)
  97 + {
  98 + //if (IS_DEBUG) printf("Field %d is %s\n", i++, token);
  99 +
  100 + tab_char[i]=token;
  101 +
  102 + printf(" %s",tab_char[i]);
  103 + i++;
  104 +
  105 + token = strsep(&string, CSV_DELIMITERS);
  106 + }
  107 + printf("\n");
  108 + }
  109 +
  110 + fclose(fp);
  111 +}
  112 +
  113 +void usage(const char * prog_name)
  114 +{
  115 + printf("Usage is %s your_csv_file\n\n", prog_name);
  116 +}
  117 +
  118 +int main(int argc, char * argv[])
  119 +{
  120 + if (2 != argc)
  121 + {
  122 + usage(argv[0]);
  123 + return 0;
  124 + }
  125 +
  126 + read_csv_file(argv[1]);
  127 + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++)
  128 + {
  129 + free(CSV_HEADER_FIELDS[i]);
  130 + }
  131 + free(CSV_HEADER_FIELDS);
  132 +
  133 + return 0;
  134 +}
0 135 \ No newline at end of file
... ...