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
|
#include <stdio.h>
#include <stdlib.h>
#include <csv.h>
struct counts {
long unsigned fields;
long unsigned rows;
};
void cb1 (void *s, size_t len, void *data) {
((struct counts *)data)->fields++; }
void cb2 (int c, void *data) {
((struct counts *)data)->rows++; }
int main(){
void *a;
void *b;
FILE *fp;
struct csv_parser p;
char buf[1024];
char dest[1024];
size_t bytes_read;
struct counts c = {0, 0};
if (csv_init(&p, 0) != 0) exit(EXIT_FAILURE);
fp = fopen("releve_bancaire.csv", "r");
if (!fp) exit(EXIT_FAILURE);
while ((bytes_read=fread(buf, 1, 1024, fp)) > 0){
if (csv_parse(&p, buf, bytes_read, cb1, cb2, &c) != bytes_read) {
fprintf(stderr, "Error while parsing file: %s\n",
csv_strerror(csv_error(&p)) );
exit(EXIT_FAILURE);
}
csv_fini(&p, cb1, cb2, &c);
csv_write(dest,1024,buf,1024)
fclose(fp);
printf("%lu fields, %lu rows\n", c.fields, c.rows);
csv_free(&p);
exit(EXIT_SUCCESS);
}
} |
Partager