gpt4 book ai didi

c - 我不知道如何使用libcs​​v中某些函数的参数

转载 作者:太空宇宙 更新时间:2023-11-04 03:41:52 25 4
gpt4 key购买 nike

所以我有一个正在进行的项目,我正在尝试使用 libcs​​v。来自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
类(class)晚会
晚会类(class)
2014-11-05irée
30.5011-05irée
增碳剂

我怎么知道哪个指针隐藏在 _void_ 后面,以及如何正确使用它(打印它,用它进行操作......)?

希望我说清楚了,非常感谢:)


我现在使用的解决方案为我提供了一个正确的字符串,我可以无误地打印或在需要时传递给 atoi()/atof() :

char *str = malloc(sizeof(char)*len);
memcpy(str, (char *)s, len);
printf("%s\n", str);

最佳答案

您在第 7 页链接到的文档中有一个很好的示例,展示了如何使用它。

回调函数 cb1 如下所示:

void cb1 (void *s, size_t len, void *data) {
}

因此您将传递一个指向 void(*)(void*, size_t, void*) 的指针。

编辑:

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.

这些字符串不是以 null 结尾的,您不能像普通的 cstrings 一样将它们传递给 printf。请尝试 printf("%.*s\n", len, 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...

void* 是指向 csv_parser 结构的数据字段的指针。它指向从文件/数据库中读取的原始数据。如果您的 CSV 包含文本,则此数据是文本,您可以将 void* 转换为 char*。如果您的 CSV 包含以原始 32 位二进制形式编码的数字(因此,不是文本),那么您可以将该 void* 转换为 uint32_t*。

大多数时候,您可能希望将其作为 char* 来读取,但了解这些文件中的内容是您的工作。解析器看到的只是一堆字节。

关于c - 我不知道如何使用libcs​​v中某些函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com