Skip to content
This repository has been archived by the owner on Jan 3, 2018. It is now read-only.

Printing out data from csv #8

Open
deseanab opened this issue Sep 18, 2016 · 1 comment
Open

Printing out data from csv #8

deseanab opened this issue Sep 18, 2016 · 1 comment

Comments

@deseanab
Copy link

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

@ivarref
Copy link

ivarref commented Oct 31, 2016

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 :-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants