从 CSV 文件中检索值
我想问,是否有任何方法可以通过使用C编程来检索最后一个for中第一列的值。
谢谢!
I want to ask, if there is any way, which I can retrieve the value of the first column in the last for, by using C programming.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设我的 .csv 文件如下所示,
这些字段为商品编号、类别编号和供应商编号。
用于读取上述 .csv 文件值的 C 代码:
Suppose my .csv file looks like this,
The fields are Item Number, Class Number and Supplier Number.
C code for reading values of above .csv file :
也许你可以看看libcsv。
Maybe you can look at libcsv.
我认为这取决于你的文件的大小。如果行不是太大,也不是太多,并且您不关心您的程序是否使用 GNU 以外的 libcs 运行,您可以使用 getline 函数将整行读入内存,直到到达最后一个。那么它应该就像找到第一个逗号一样简单。
如果您有一个由小行组成的大文件,您可以 fseek 到文件末尾并逐字节备份,直到找到 '\n'。然后你就到了最后一行的开头。
如果文件和行都很大,也许对整个文件执行 mmap 到文件末尾,然后读回 '\n' 可能比所有这些 fseek 更有效。尽管 mmap 的可移植性可能不如 fseeks(因为 fseek 是标准 C)。
I think it depends on the size of your file. If the lines are not too big, there are not too many of them and you don't care if your program doesn't run with libcs other than GNU, you could use getline function to read entire lines into memory until you reach the last. Then it should be as simple as finding the first comma.
If you have a large file composed of small lines, you could fseek to the end of the file and back up byte per byte until you find a '\n'. Then you are at the beginning of the last line.
If both the file and lines are huge, perhaps doing a mmap on the entire file going to the end of it and then reading back to the '\n' might be more efficient than all those fseeks. Although mmap is arguably less portable than fseeks (since fseek is standard C).