-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcsv-c.c
More file actions
27 lines (24 loc) · 682 Bytes
/
csv-c.c
File metadata and controls
27 lines (24 loc) · 682 Bytes
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
/* #include <libcsv/csv.h>*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <csv.h>
void field_count (void* str, size_t str_len, void* data) {
int* count = (int*)data;
*count += 1;
}
const int READ_SZ = 1024 * 1024;
int main (int argc, char* argv[]) {
struct csv_parser parser = {0};
csv_init (&parser, CSV_APPEND_NULL);
char *buf = (char*)malloc (READ_SZ);
size_t buflen = READ_SZ;
int count = 0;
while ((buflen = read (0, buf, READ_SZ)) > 0) {
csv_parse (&parser, buf, buflen, field_count, 0, &count);
}
printf ("%d\n", count);
free (buf);
csv_free (&parser);
return EXIT_SUCCESS;
}