如何使用C解析文件? [英] How to parse a file using C?
本文介绍了如何使用C解析文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是文件样本。
Here is the file sample.
########################################### Configuration File for Employee Information ##########################
#################################
ID = 101
Name = Anjum ####### She works for Innobox
sal= 30000# per month
Experience = 5.4
#################
ID = 102
Name = Satya ####### He works for Innobox
sal= 60000# per month
Experience = 7.4
####################
ID = 103
Name = Ramana #######
sal= 70000# per month
Experience = 8.4
####################
ID = 104
Name = Trinadh ####### ## He works for Innobox
sal = 60000# per month
Experience = 7.0
######################
i需要解析它并将所有数据存储在结构中。打印。
结构成员是 -
1.ID
2.name
3.salary
4.experience
任何条款?
i need to parse it and store all data in a structure. and print.
structure members are-
1.ID
2.name
3.salary
4.experience
Any claue?
推荐答案
不,我不喜欢潜入评论中的所有代码。
但这应该给你一个第一个想法:
a。)逐行阅读
b。)从第一次出现#到行尾删除每一行,因为#似乎开始发表评论。
c。)如果转换线不为空且包含=,则评估剩余的a = b
不是可以解决的问题使用imediatelly,但也许有一些关于逻辑的提示。
请参阅解决方案#3了解实现提示。
No I don't like to dive trough all the code in your comment.
But this should give you a first idea:
a.) Read line by line
b.) Delete for each line from first occurence of "#" to the end of line, becuase "#" seems to start comment.
c.) In case remaing line is not empty and contains a "=" then evaluate remaining "a = b"
Not a solution which can be used imediatelly, but maybe some hints about the logic.
See solution #3 for implementation hints.
您的语言可能是在伪EBNF中定义,如
Your language might be defined in "pseudo" EBNF like
File = { Skip } { Record } .
Record = Field<ID> Field<Name> Field<Sal> Field<Experience> .
Field<Key> = { WS } Key { WS } "=" { WS } Text { Skip } .
Skip = { WS } [HASH { ANY-CHAR-EXCEPT-NL } ] NL .
Text = ANY-CHAR-EXCEPT-HASH-WS { ANY-CHAR-EXCEPT-HASH-WS } .
按照Bruno Sprecher所描述的步骤在解决方案#2中。
您可以使用C函数
- strcspn
- strtok
- strncpy
这篇关于如何使用C解析文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文