【发布时间】:2015-01-06 22:33:22
【问题描述】:
所以我有一个正在进行的项目,我正在尝试使用libcsv。
从doc,我了解到csv_parse() 需要
"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()."
我给 csv_parse 作为回调调用的函数(在读取现有文件并尝试用它填充结构时)是:
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);
}
而且我不知道如何使用该void *s,因为我的数据库中有多种数据类型,例如 char * 和 int等等...
目前我只想能够正确打印它,我的代码在第一次调用该函数时工作,但在那之后,字符串(当然是char的数组)长度存在问题。
示例:
2014-11-04
07.5011-04
课程晚会
晚会
2014-11-05入围
30.5011-05irée
carburant5irée
如何知道_void_后面隐藏了哪个指针,以及如何正确使用(打印、操作...)?
希望我说的很清楚,非常感谢:)
我现在使用的解决方案可以让我得到一个正确的字符串,我可以在需要时无错误地打印或传递给 atoi()/atof():
char *str = malloc(sizeof(char)*len);
memcpy(str, (char *)s, len);
printf("%s\n", str);
【问题讨论】:
标签: c pointers csv callback void