On 26 Jun, 04:03, mike.rosset+g...@gmail.com wrote:
I am new to C and I have a program design question. Not only am I trying
to learn C but I'm also trying to pick up good habits.
good
:-)
And I'm cocerned
with doing things the right [way] and avoiding picking up bad habits.
The program I'm working on uses popen
popen() isn't actually standard C. It's standard Unix (Posix).
But your question becomes a standard C question if we assume you're
reading
from a stream (eg. opened by fopen())
to read some pre-formatted output
from another program. The output looks like this.
that is the *input* to your program looks like:
line 0: persons_name
line 1: persons_last_name
line 2: .....
I then need to take each line assign it to a structure member. What
would be the simplest approach to find the member field from the line
loop count?
I'm not sure. What are the structure members called?
When I read your question I thought persons_name
indicated a name and the actual input looked like
line 0: frodo
line 1: baggins
etc.
So you might be doing something like
void process_lines (struct Record *record)
{
char line [LARGEST_LINE];
char field [LARGEST_FIELD];
int line_count = 0;
while (read_line(line) == OK)
{
line_count++;
get_field (field, line);
switch (line_count)
{
1:
strcpy (record->person_name, field);
break;
2:
strcpy (record->persons_last_name, field);
break;
default:
fprintf (stderr, "Dire Straits!\n");
exit (EXIT_FAILURE);
}
}
}
which isn't exactly elegant...
The error handling is weak, the program is very trusting of the input
source.
I'd do get_field() more like this (a different approach to
RH's solution).
int get_field (char *field, const char *line)
{
int line_count
if (sscanf (line, "line %d: %s", &line_count, field) != 2)
return PARSE_ERROR;
return OK;
}
Could you explain your problem in more detail?
--
Nick Keighley
/*
- Note:
- I wanted to put a check in getGuiExtract(), but it's a void
method, inherited from the father of the father of the father of
the
[...]
the father of the father of the father of the father of the
father ...
So I can't modify its interface.
*/
[cry for help found in source code]