473,799 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing config file

If I am parsing a config file that uses '#' for comments and the
config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is
it recommended to use

a) fgetc (parse a character at a time)
b) fgets (read in blocks of whatever size)
c) fread (get the size of the file and fread the entire thing into
memory)

and when would it be appropriate to use either a, b, or c?
nethlek
Nov 13 '05 #1
9 23609
Mantorok Redgormor <ne*****@tokyo. com> scribbled the following:
If I am parsing a config file that uses '#' for comments and the
config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is
it recommended to use a) fgetc (parse a character at a time)
b) fgets (read in blocks of whatever size)
c) fread (get the size of the file and fread the entire thing into
memory) and when would it be appropriate to use either a, b, or c?


If the config file's format is so that each VARIABLE=VALUE is on a
separate line, I definitely recommed b) fgets. Otherwise you're best off
with c) fread, but the problem is, you'll have to parse the delimiters
out yourself.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
Nov 13 '05 #2
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
If the config file's format is so that each VARIABLE=VALUE is on a
separate line, I definitely recommed b) fgets. Otherwise you're best off
with c) fread, but the problem is, you'll have to parse the delimiters
out yourself.


Why not

fscanf( "%[^#=]=%s", &variable, &value );

?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #3
On Tue, 16 Sep 2003 16:50:56 UTC, ne*****@tokyo.c om (Mantorok
Redgormor) wrote:
If I am parsing a config file that uses '#' for comments and the
config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is
it recommended to use

a) fgetc (parse a character at a time)
Maybe a good choice because you handles each char that comes in
directly. No need to mess up with buffer sizes for a whole line.
b) fgets (read in blocks of whatever size)
Maybe a good choice because you can thereafter handle the line as such
as you likes.
Maybe a bad choice because it may be possible that the buffer you
gives fgets is too small
c) fread (get the size of the file and fread the entire thing into
memory)
Maybe a good choice when you knows the whole size of the file. Anyway
it costs more memory as absolutely required.

and when would it be appropriate to use either a, b, or c?


Does you like to handle undersized input buffers? Then use b).
Does you have quick access to the size of the file? Then use c)
Don't you like to handle dynamic input buffers only to get a line
coplete because it is longer than you had think it should be? And is
your memory limited in size (wheras your progam may not the only that
runs on the mashine)?
Or is it even not so easy to determine the size of the file in a
manner that you can allocate a buffer big enough to read it in at
once?
If the anywer you gives to one of the questions above is yes then a)
is your choice.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #4
The Real OS/2 Guy wrote:

On Tue, 16 Sep 2003 16:50:56 UTC, ne*****@tokyo.c om (Mantorok
Redgormor) wrote:
If I am parsing a config file that uses '#' for comments and the
config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is
it recommended to use

a) fgetc (parse a character at a time)


Maybe a good choice because you handles each char that comes in
directly. No need to mess up with buffer sizes for a whole line.
b) fgets (read in blocks of whatever size)


Maybe a good choice because you can thereafter handle the line as such
as you likes.
Maybe a bad choice because it may be possible that the buffer you
gives fgets is too small
c) fread (get the size of the file and fread the entire thing into
memory)


Maybe a good choice when you knows the whole size of the file. Anyway
it costs more memory as absolutely required.

and when would it be appropriate to use either a, b, or c?


Does you like to handle undersized input buffers? Then use b).
Does you have quick access to the size of the file? Then use c)
Don't you like to handle dynamic input buffers only to get a line
coplete because it is longer than you had think it should be? And is
your memory limited in size (wheras your progam may not the only that
runs on the mashine)?
Or is it even not so easy to determine the size of the file in a
manner that you can allocate a buffer big enough to read it in at
once?
If the anywer you gives to one of the questions above is yes then a)
is your choice.

Herbert, I disagree. Choice b) is the only choice. Choice a) is too ugly
for a mother to love. Choice c), fread() a text file and then parse it,
uses lots of memory and complicates things more than necessary.

The configuration file as described, defines variables in 'key=value'
format, line at a time. It is fgets() that reads a file 'line at a
time'. It is trivial to determine comment lines beginning with '#' or';'
or whatever and skip them.

Everyone please note that in order to read any file correctly, you must
know how it was written, ie. its format. There are 'rules' to writing
..cfg or .ini (or other) files which you must know exactly before you can
read them successfully.
--
Joe Wright mailto:jo****** **@earthlink.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5
On Tue, 16 Sep 2003 17:26:12 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote in comp.lang.c:
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
If the config file's format is so that each VARIABLE=VALUE is on a
separate line, I definitely recommed b) fgets. Otherwise you're best off
with c) fread, but the problem is, you'll have to parse the delimiters
out yourself.


Why not

fscanf( "%[^#=]=%s", &variable, &value );

?


Because any *scanf with "%s" lacking a size specifier is just another
name for gets(), a nasty buffer overrun just waiting to happen.

Thus are worms born...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6

Joe Wright <jo********@ear thlink.net> wrote in message
news:3F******** *@earthlink.net ...
The Real OS/2 Guy wrote:

On Tue, 16 Sep 2003 16:50:56 UTC, ne*****@tokyo.c om (Mantorok
Redgormor) wrote:
If I am parsing a config file that uses '#' for comments and the
config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is
it recommended to use

a) fgetc (parse a character at a time)


Maybe a good choice because you handles each char that comes in
directly. No need to mess up with buffer sizes for a whole line.
b) fgets (read in blocks of whatever size)


Maybe a good choice because you can thereafter handle the line as such
as you likes.
Maybe a bad choice because it may be possible that the buffer you
gives fgets is too small
c) fread (get the size of the file and fread the entire thing into
memory)


Maybe a good choice when you knows the whole size of the file. Anyway
it costs more memory as absolutely required.

and when would it be appropriate to use either a, b, or c?


Does you like to handle undersized input buffers? Then use b).
Does you have quick access to the size of the file? Then use c)
Don't you like to handle dynamic input buffers only to get a line
coplete because it is longer than you had think it should be? And is
your memory limited in size (wheras your progam may not the only that
runs on the mashine)?
Or is it even not so easy to determine the size of the file in a
manner that you can allocate a buffer big enough to read it in at
once?
If the anywer you gives to one of the questions above is yes then a)
is your choice.

Herbert, I disagree. Choice b) is the only choice. Choice a) is too ugly
for a mother to love. Choice c), fread() a text file and then parse it,
uses lots of memory and complicates things more than necessary.

The configuration file as described, defines variables in 'key=value'
format, line at a time. It is fgets() that reads a file 'line at a
time'. It is trivial to determine comment lines beginning with '#' or';'
or whatever and skip them.

Everyone please note that in order to read any file correctly, you must
know how it was written, ie. its format. There are 'rules' to writing
.cfg or .ini (or other) files which you must know exactly before you can
read them successfully.


And robust code that reads them should be able to handle
corrupt or incorrectly formatted data (e.g. by assuming
'defaults', or giving an error message, terminating, etc.)

-Mike

Nov 13 '05 #7
Jack Klein <ja*******@spam cop.net> spoke thus:
Because any *scanf with "%s" lacking a size specifier is just another
name for gets(), a nasty buffer overrun just waiting to happen.


Well, considering the OP was just parsing a config file, the chances for an
exploit shouldn't be too high, eh? Thanks, though, for I had forgotten about
that... Although something like

fscanf( "%20[^=]s=%20s", &s1, &s2 );

would fail if it weren't given exactly 20 characters for the first string,
right...?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #8
Jack Klein <ja*******@spam cop.net> spoke thus:
Because any *scanf with "%s" lacking a size specifier is just another
name for gets(), a nasty buffer overrun just waiting to happen.


Well, considering the OP was just parsing a config file, the chances for an
exploit shouldn't be too high, eh? Thanks, though, for I had forgotten about
that... Although something like

fscanf( "%20[^#=]s=%20s", &s1, &s2 );

would fail if it weren't given exactly 20 characters for the first string,
right...? Maybe I should just be quiet now...

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #9
On Wed, 17 Sep 2003 16:30:22 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
Jack Klein <ja*******@spam cop.net> spoke thus:
Because any *scanf with "%s" lacking a size specifier is just another
name for gets(), a nasty buffer overrun just waiting to happen.


Well, considering the OP was just parsing a config file, the chances for an
exploit shouldn't be too high, eh? Thanks, though, for I had forgotten about
that... Although something like

fscanf( "%20[^#=]s=%20s", &s1, &s2 );

would fail if it weren't given exactly 20 characters for the first string,
right...? Maybe I should just be quiet now...


Not right; no comment on whether you should. A width specifier on any
*scanf conversion is an upper limit, although %Nc will always read to
the upper limit or end-of-input/error.

Also the %20s on the right side won't allow whitespace in the value,
which I would want to; %20[^\n] will. And either of those will
normally leave the newline in the input stream, which is probably OK
if you want to handle # lines with a getc or similar rather than
another (prior?) fscanf; either will also leave any text exceeding the
limit, and %20s any text following a whitespace; adding %*[^\n] would
reduce the number of different cases you have to handle.

- David.Thompson1 at worldnet.att.ne t
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3831
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply* access config files. This is version 3, which is a big overhaul. It extends ConfigObj to reading config files with sections and various other simplifications. I find ConfigObj extremely easy to use and use it for reading config files and data...
3
1291
by: Aaron Walker | last post by:
At the beginning of my program, I open a config file and load the contents into a structure (please disregard the non-portable sockaddr_in struct as it is irrelevant to the problem): struct conf_data { char *config_file; char *root; /* SERVER_ROOT */ char *pid_file; /* PID_FILE */ char *log_dir; /* LOG_DIR */ struct sockaddr_in local_addr;
3
1953
by: Dave Cullen | last post by:
I'm a C# noob trying to maintain and modify someone else's code. The previous programmer used a config file to load parameters for a database connect string. The file is named app.config and it's in the project and under VSS source control. I'd like to rename the file to something more appropriate. I can't find any reference to that filename in the project, and I don't know when or how the data gets loaded. I'd like someone to...
10
2556
by: NuB | last post by:
I'm creating a C# class file(DLL) that will be used by an asp.net application. In the DLL I want to read a web.config, or app.config file so some information can change without having to go into the code itself. How can I have the dll read a app.config or web.config file for data? The information I'm looking to store in the file is servernames, file locations, etc. thanks
16
1996
by: Timm | last post by:
I'm trying to use ASP objects (basically formed and populated based on Web.Config settings) and I want to use them in a different non-asp program with minimal reprogramming. So, my question is how do I mock up what ASP does to read the Web.Config file? I've tried messing around in the System.Configuration library and haven't been able to find anything from taking a quick look at the objects and their methods (for the goal of populating the...
3
4604
by: =?Utf-8?B?RGFuYQ==?= | last post by:
I am re-posting this message after registering my posting alias. When I specify an end tag for the clear element of namespaces in my web.config file, the parser error "Unrecognized element 'add'" is reported. .... <pages> <namespaces> <clear></clear> <add namespace="System"/>
3
2933
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it brings me to the dir: C:\Visual Studio 2005\WebSites\WebSite1, it creates default.aspx and default.aspx.vb and I pasted the following code into default.aspx. I go to build to build web site and it says:------ Build started: Project:
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.