寻找一个解析CSV的C程序 [英] Looking for a C program to parse CSV

查看:53
本文介绍了寻找一个解析CSV的C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我需要解析的Excel电子表格。我想把这个保存为一个CSV文件。

然后使用C读取文件。实际的EXCEL

看起来像:

a,b ab a,b a,,b


CSV格式如下:

a,b,a,b,a,,, ; b"," a,"",b"


是否有人有建议或有基于C程序的代码来解析CSV。

请回复留言板本身。我不希望收到垃圾邮件。

I have an excel spreadsheet that I need to parse. I was thinking of saving
this as a CSV file. And then reading the file using C. The actual in EXCEL
looks like:
a,b a"b a","b a,",b

In CSV format looks like:
"a,b","a""b","a"",""b","a,"",b"

Does anybody have suggestions or have C program based code to parse CSV.
Please reply to the message board itself. I do not wish to get spam.

推荐答案

vvk4< vv ** @ nospam.mail.com>写道:
vvk4 <vv**@nospam.mail.com> wrote:
是否有人有建议或有基于C程序的代码来解析CSV。
请回复留言板本身。我不希望收到垃圾邮件。
Does anybody have suggestions or have C program based code to parse CSV.
Please reply to the message board itself. I do not wish to get spam.




用fgets()读取文件中的字符串,然后用strtok解析字符串

(并且随心所欲地做任何事情。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Read the strings from the file with fgets(), then parse the strings
with strtok() and do whatever you like with them.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


vvk4写道:
我有一个excel电子表格,我需要解析。我想把这个保存为CSV文件。然后使用C读取文件.EXCEL中的实际内容如下:
a,b ab a,b a,,b

CSV格式看起来像:
a,b,a,b,a,,b,a,b,a,b,a,b,a,


是的,这是一种明智的方法。

是否有人有建议或有基于C程序的代码来解析CSV。


是的,用标准C编写程序并在此处发布任何问题。这个

不是一个想要来源的小组,它是一个讨论语言的小组。

请回复留言板本身。


这不是留言板,而是新闻组。您使用

网页界面的事实并没有改变这一点,大多数用户都*不*使用网页

界面。

我不希望收到垃圾邮件。
I have an excel spreadsheet that I need to parse. I was thinking of saving
this as a CSV file. And then reading the file using C. The actual in EXCEL
looks like:
a,b a"b a","b a,",b

In CSV format looks like:
"a,b","a""b","a"",""b","a,"",b"
Yes, this is a sensible approach.
Does anybody have suggestions or have C program based code to parse CSV.
Yes, write a program in standard C and post here with any problems. This
is not a sources wanted group, it is a group for discussing the language.
Please reply to the message board itself.
This is not a message board, it is a news group. The fact that you use a
web interface does not change this, and most users do *not* use a web
interface.
I do not wish to get spam.




我们不希望脱离主题帖子。您是否在发布此处之前阅读了常见问题解答,欢迎

消息以及几天的帖子?我想不是。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,它是真实的,我读了它。



We don''t wish to get off topic posts. Did you read the FAQ, welcome
message, and a few days worth of posts before posting here? I think not.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


在文章< di ********** @ chessie.cirr.com>,

Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:
In article <di**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:


vvk4< vv ** @ nospam.mail.com>写道:


vvk4 <vv**@nospam.mail.com> wrote:
是否有人有建议或有基于C程序的代码来解析CSV。
请回复留言板本身。我不希望收到垃圾邮件。
Does anybody have suggestions or have C program based code to parse CSV.
Please reply to the message board itself. I do not wish to get spam.



用fgets()读取文件中的字符串,然后用strtok()解析字符串
并用它们做任何你喜欢的事情。



Read the strings from the file with fgets(), then parse the strings
with strtok() and do whatever you like with them.




我认为这对于原始海报的文件类型是错误的建议

。 strtok()并不理解引号,而且它将

分隔符的运行视为一个单元,因此CSV文件中的空字段可能会无意中跳过
。你可以使用strtok,但是你必须做很多工作来克服它的局限。


更好的建议可能只是看看每个字符串中的字符

从左到右并跟踪你是否在引用的字符串中或

不是。当你到达一个不带引号的逗号时,那就是一个字段分隔符。

那个,加上转向"进入引号内部将足够并且可能比strtok()更简单。


一路上,人们可能会学习解析的内容,这可能是

比代码本身更有趣。



I think this is bad advice for the kind of file the original poster had
in mind. strtok() doesn''t understand quotes, plus it treats runs of
separators as a single unit so empty fields in a CSV file can be
inadvertently skipped. You can use strtok, but you''ll have to do quite
some work to overcome its limitations.

Better advice might be to just look at each character in the string
left to right and keep track of whether you''re in a quoted string or
not. When you reach an unquoted comma, that''s a field separator.
That, combined with turning "" into " inside of quotes would be
sufficient and probably simpler than strtok().

Along the way, one might learn things about parsing, which is perhaps
more interesting than the code itself.


这篇关于寻找一个解析CSV的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆