So I have an ongoing project where I am trying to use libcsv
. From the doc , I understand that csv_parse()
takes
"a pointer to a callback function (cb1) that will be called from csv_parse()
after an entire field has been read. cb1 will be called with a pointer to the
parsed data (which is NOT nul-terminated unless the CSV_APPEND_NULL option is
set), the number of bytes in the data, and the pointer that was passed
to csv_parse()."
The function I gave to csv_parse to call as a callback (while reading an existing file, and trying to populate a struct with it) is :
void cb1_db_populate(void *s, size_t len, void *data)
{
int row = ((struct DB *)data)->currRow, field = ((struct DB *)data)->currField;
str = (char *)s;
printf("%s\n", str);
}
And I have no clue on how to use that void *s
since there is several data type in my data base , like char *
and int
and so on... For the moment I would just like to be able to print it properly, the code I have works the first time the function is called, but after that, there is a problem with the string's (array of char
of course) length.
Sample :
2014-11-04
07.5011-04
courses soirée
eourses soirée
2014-11-05irée
30.5011-05irée
carburant5irée
How can I know which of pointer hides behind _void_
, and how to use it properly (print it, make operations with it...) ?
Hope I'm being clear, thank you very much :)
The solution I now use that gets me a proper string, that I can print without error or pass to atoi()/atof() when I need to :
char *str = malloc(sizeof(char)*len);
memcpy(str, (char *)s, len);
printf("%s\n", str);
There's a nice example in the documentation you linked to on page 7 that shows how to use it.
The callback function cb1
looks like this :
void cb1 (void *s, size_t len, void *data) {
}
So you'd be passing a pointer to a void(*)(void*, size_t, void*)
.
EDIT:
For the moment I would just like to be able to print it properly, the code I have works the first time the function is called, but after that, there is a problem with the string's (array of char of course) length.
Those strings are not null-terminated, you can't just pass them to printf like normal cstrings. Try printf("%.*s\\n", len, str);
instead.
And I have no clue on how to use that void *s since there is several data type in my data base, like char * and int and so on...
The void* is a pointer to the data field of a csv_parser struct. It points to the raw data read from the file/database. If your CSV contains text, then this data is text and you can cast the void* to char*. If your CSV contains numbers encoded in raw 32bit binary form (so, not text), then you can cast that void* to a uint32_t*.
Most of the time, you'll probably want to read it as a char*, but it's your job to know what's inside those files. All the parser sees is a bunch of bytes.
void *s
but it's likely you may find it useful later on like if you discover that using global variables in the callback function was a bad idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.