Jonas Geiregat wrote:
I'm trying to parse a configuration file,
this is the first time I'm trying something like this, and it seems hard.
C's I/O functions are a mess and hard to use , that is if you want to
write code in a good way.
I've tried google many times looking for some good document article that
talks about C I/O and parsing files, most of the docs that I came across
just talk about basic I/O things I can also learn from reading my man pages.
Any kind of advise is welcome!
It would help a bit if you could tell us what kind of problems you're
running into; what's the structure of the file you're trying to parse,
for example?
Advice on C I/O routines:
1. Avoid gets() like the plague. Better yet, just pretend gets()
never existed. Using it *will* introduce a point of failure in your
code.
2. Unless your input is relatively simple in structure and *always*
well-formed, don't use scanf() to read and assign items directly from
the stream; scanf() doesn't give you the ability to easily recognize or
recover from malformed input.
3. Use fgets() to read in lines of input, and then process the
resulting buffer with strtok(), strstr(), strchr(), sscanf(), or some
combination of the above. Alternately, use fgetc() to read in a single
character at a time and process via state tables, but unless you're
writing a compiler front-end or something similar, that's probably
overkill. And if you *are* writing a compiler front-end, use lex and
yacc or something similar instead of rolling your own.
4. Break your parsing code into several layers of abstraction, with
the C I/O routines wrapped in more abstract operations like
GetNextLine() or GetNextToken(), which are in turn called by
progressively more abstract operations like GetLogfileName() or
GetStartupParams(). This makes code a bit easier to write; you're only
focusing on as much complexity as you need to at any particular level.
It also allows you to make changes at the native input level without
hacking the application logic.
5. Before starting *any* of this, make sure there isn't a tool already
out there to do what you're doing. For example, if your input is in
XML format, there are already a number of parser libraries available
for parsing XML documents. And there are tools like the previously
mentioned lex and yacc (flex and bison in the GNU world) that allow you
to build robust and sophisticated parsers.
6. Of course, make sure you're using the right tool for the job in the
first place. The old joke goes something like "when confronted with a
problem, a programmer says, 'I know, I'll use XML.' The programmer now
has two problems. " XML's wonderful, but do you *really* need to link
in a heavyweight DOM parser to handle 5 lines of text? Do you really
need to go through the hassle of generating code from lex and yacc if
all you're dealing with is a comma-delimited list of name-value pairs?
This is why it's important for you to give us some idea of what you're
trying to do.