libcsv icon indicating copy to clipboard operation
libcsv copied to clipboard

Printing out data from csv

Open deseanab opened this issue 9 years ago • 1 comments

Hi Rob, great job on the library, super useful.

Question - how do I retrieve the data stored in my buffer. I have a csv of integers and the output doesn't make much sense to me.

The data looks like this in the csv:

420 | 249 680 | 487 249 | 420

After I open the file I iterate and parse:

while((bytes_read = fread(buf, 1, 1024, knows_input_file)) > 0) { //printf(" %s %d \n", " Buf: ", buf); csv_parse(&parser, buf, bytes_read, cb1, NULL, NULL);

}

The callback just prints out the buffer (which I've made global for now)

char buf[1024];

void cb1 (void *s, size_t friend_size, void * friend_id ) { int i = 0; while(i < sizeof(buf)/sizeof(char)){ printf(" %s %d \n", "Character: ", buf); i++; } }

The integers returned back doesn't look like the data and I'm trying to make sense of where these characters/integers are being stored

deseanab avatar Sep 18 '16 18:09 deseanab

Hi.

So void *s points to your characters in the CSV file. You need to parse that void*. Example parsing code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/*
gcc sample_parse.c --std=c99 && ./a.out
*/
int main(int argc, char** argv) {
    char buffer[] = "12345";
    char* dest;
    long number = strtol(buffer, &dest, 10);
    if (dest==buffer) {
        fprintf(stderr, "not a number\n");
        exit(EXIT_FAILURE);
    } else {
        printf("Got number %ld\n", number);
    }
    return 0;
}

Good luck :-)

ivarref avatar Oct 31 '16 13:10 ivarref