检索 csv 文件的列 ID
我想问是否有任何方法可以使用C编程检索csv文件的列号?默认情况下在 csv 文件中设置的数字以及行名称,即:
AB C............ Z AA AB AC............
I want to ask if there is any way through which I can retrieve column numbers of a csv file using C programming? Those numbers which are by default set in csv file and also the row names i-e :
A B C.............. Z AA AB AC...........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,兰迪,但你对此有一些很大的误解。 CSV 文件几乎是文本文件,就像您在记事本中创建的那样,单词/值之间恰好有逗号。如果您想用 C 语言读取它们,您可以使用 scanf() 解析每一行 - 要么:
编写一个只能使用一种特定字段布局的程序:如...
尝试将字段解析为各种类型,直到失败:
编辑以响应下面的评论/问题:
抱歉 - 我只是不明白你在问什么。什么是“增加”数据?如果您的意思是该文件具有代表不同行的不同行,并且具有多个列,那么是的,这是正常的,可以通过我上面列出的方法进行解析。如果您想问“如何保存行/列以供将来处理”,那么您可以使用字段创建一个结构(如果您知道想要对列名称和类型进行硬编码),或者使用数组(可能是最初是字符数据)。然后,您可以拥有这些结构/数组的数组(或链表,如果您有相应的库)。例如:
这将读取(从标准输入 - 如果您想直接从命名文件读取,请切换到使用 fopen() 和 fscanf())最多 MAX_LINES “Person”行。
如果您不知道数据的数量和类型,则需要使用我之前列出的替代(更复杂)方法:尝试传递每个字段直到失败,检查以逗号分隔的字段结尾与结尾字段文件外。我的脑海中浮现出未经测试的东西,比如:
Sorry randy, but you've got some big misconceptions about this. CSV files are pretty much text files like you'd create in notepad that happen to have commas between the words/values. If you want to read them in C, you can parse each line using scanf() - either:
write a program that can only work with one particular layout of fields: as in...
try parsing the fields as various types until you get a failure:
Edit in response to comment/question below:
Sorry - I just don't understand what you're asking. What is "increasing" data? If you mean the file has different lines representing distinct rows, and has more than one column, then yes that's normal and it can be parsed by the approach I listed above. If you mean to ask "how can you save the rows/columns for some future processing", then you can create a structure with the fields (if you know want to hardcode the column names and types), or use an array (perhaps of char data initially). You can then have an array (or linked list if you have a library for that) of those structures/arrays. For example:
This would read (from standard input - switch to using fopen() and fscanf() if you want to read directly from a named file) up to MAX_LINES "Person" rows.
If you don't know the number and types of data you need to use the alternative (more complicated) approach I listed earlier: trying to pass each field until there's a failure, checking for end-of-comma-separated fields versus end-of-file. Off-the-top-of-my-head and untested, something like: