Newbie question on loading data from config file

P

philbo30

Silly question, please excuse my ignorance. This board is a wonderful
resource and I feel obligated to use it. In advance, thanks to all who
help out here.

Here's the situation:
Currently, a config file, "config.txt" has the following 3 numbers
inside it:
..124 .131 .104

The code line to load these numbers for use looks like this: [Works
great]
fscanf(config, "%lf %lf %lf", &abc, &def, &ghi);


However, I want my config file to look like this:
..124
..131
..104

and to use fscanf or fgets to read line by line and load to &abc, &def,
&ghi

This is for an embedded system, so most simple is most best. In
advance, thanks for any assistance. -Phil
 
K

Keith Thompson

philbo30 said:
Silly question, please excuse my ignorance. This board is a wonderful
resource and I feel obligated to use it. In advance, thanks to all who
help out here.

Here's the situation:
Currently, a config file, "config.txt" has the following 3 numbers
inside it:
.124 .131 .104

The code line to load these numbers for use looks like this: [Works
great]
fscanf(config, "%lf %lf %lf", &abc, &def, &ghi);


However, I want my config file to look like this:
.124
.131
.104

and to use fscanf or fgets to read line by line and load to &abc, &def,
&ghi

This is for an embedded system, so most simple is most best. In
advance, thanks for any assistance. -Phil

fscanf, with most formats, skips leading whitespace -- including
newlines.

I usually recommend using fgets() to read a line at a time, and
fscanf() to parse each line, but if you control the input format
yourself, your fscanf() call should work with either

..124 .131 .104

or

..124
..131
..104
 
J

Joe Wright

philbo30 said:
Silly question, please excuse my ignorance. This board is a wonderful
resource and I feel obligated to use it. In advance, thanks to all who
help out here.

Here's the situation:
Currently, a config file, "config.txt" has the following 3 numbers
inside it:
.124 .131 .104

The code line to load these numbers for use looks like this: [Works
great]
fscanf(config, "%lf %lf %lf", &abc, &def, &ghi);


However, I want my config file to look like this:
.124
.131
.104

and to use fscanf or fgets to read line by line and load to &abc, &def,
&ghi

This is for an embedded system, so most simple is most best. In
advance, thanks for any assistance. -Phil

Seems straightforward. Make it happen. What are you asking about?
 
P

philbo30

It works; I was just trying to get a jump on today's work with
overnight assistance. The second task is to take the config file as it
looks now:

..124
..131
..104

and put a description to the right of the number, so it makes sense to
technicians who may have to work on the system.

So, I really want the config file to look something like this:

..124 //Proportion of ice cream
..131 //Proportion of soup
..104 //Proportion of pizza


Although, if this adds too much code, I'm going to let it go for now.

-Phil






Joe said:
philbo30 said:
Silly question, please excuse my ignorance. This board is a wonderful
resource and I feel obligated to use it. In advance, thanks to all who
help out here.

Here's the situation:
Currently, a config file, "config.txt" has the following 3 numbers
inside it:
.124 .131 .104

The code line to load these numbers for use looks like this: [Works
great]
fscanf(config, "%lf %lf %lf", &abc, &def, &ghi);


However, I want my config file to look like this:
.124
.131
.104

and to use fscanf or fgets to read line by line and load to &abc, &def,
&ghi

This is for an embedded system, so most simple is most best. In
advance, thanks for any assistance. -Phil

Seems straightforward. Make it happen. What are you asking about?
 
S

Szabolcs Borsanyi

philbo30 said:
So, I really want the config file to look something like this:

.124 //Proportion of ice cream
.131 //Proportion of soup
.104 //Proportion of pizza

Then first
fscanf(file,"%lf",&doublevar);
then
while(fgets(buf,size,file) && buf[strlen(buf)-1]!='\n');
/*sorry if there are bugs or typos */
and ignore the contents of buf.

Szabolcs Borsanyi
 
K

Keith Thompson

philbo30 said:
It works; I was just trying to get a jump on today's work with
overnight assistance. The second task is to take the config file as it
looks now:

.124
.131
.104

and put a description to the right of the number, so it makes sense to
technicians who may have to work on the system.

So, I really want the config file to look something like this:

.124 //Proportion of ice cream
.131 //Proportion of soup
.104 //Proportion of pizza
[...]

Please don't top-post. See <http://www.caliburn.nl/topposting.html>
(and the vast majority of followups in this newsgroup).

You could probably use fscanf() to read the number, then use something
else to skip the rest of the line, but your file format is becoming
complex enough that it will probably make more sense to use fgets() to
read an entire line, then sscanf() to parse it.

Think about error handling. Is the comment required? If so, what
should the program do if it's missing? Should blank lines be ignored?
Is whitespace between the number and the comment required? What if
the comment begins with something other than '#'? And so forth.

Once you've rigorously defined your file format, writing the code
to parse it should be easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top