Re: Reading a table




Roland Pibinger <rpbg123@xxxxxxxxx> wrote in message
news:474f17f2.627932@xxxxxxxxxxxxxxxxx
On Tue, 27 Nov 2007 02:19:55 GMT, "Bill Reid" wrote:
Anyway...

#include <stdio.h>
#include <stdlib.h>

#define LINEMAX 512

extern unsigned
assign_text_file_line_fields
(char *,unsigned (*)(unsigned,char *,void *),void *);

static FILE *text_file;

static char text_line[LINEMAX];

/* assigns field values from text file lines using callback function */
unsigned assign_text_file_line_fields
(char *text_file_path,
unsigned assign_func(unsigned,char *,void *),void *assign_ptr) {
unsigned line_num=0;
ret_val=FALSE;

/* try to open text file, return if failure */
if((text_file=fopen(text_file_path,"rt"))==NULL) {
printf("\nERROR: Could not open text file\n%s",
text_file_path);
goto EXIT_FUNCTION;
}

/* get every line in file, pass to callback function */
while((fgets(text_line,LINEMAX,text_file))!=NULL) {

if(!assign_func(line_num,text_line,assign_ptr)) {
printf("ERROR: Could not assign line %d fields);
goto CLOSE_TEXT_FILE;
}

line_num++;
}

/* OK, looks like we've succeeded */
ret_val=TRUE;

/* try to close file, warn if failure */
CLOSE_TEXT_FILE :
if((fclose(db_init_file))==EOF)
printf("\nWARNING: Problem closing text file\n%s",
text_file_path);

/* buh-bye */
EXIT_FUNCTION :
return ret_val;
}

Your callback solution has its merits. But you could have done it

- without gotos

They're worth it just to irritate some people and don't hurt
anything...

- whithout static FILE

Possibly, but the idea here is just to read a single file and
then close it right up...

- for arbitrary long lines (no hard-coded maximum line lenght)

Yes, but again the idea here is we KNOW the maximum size
of our file lines, almost certainly because we wrote them in the
first place with trusted and checked data (note that I left out anything
about sizing the array for the number of lines, that's left "up to the
reader")...in real life, I do have a similar function that does dynamic
resizing for arbitrarily long lines for other types of files...

- with a typedef for the callback function for better readability

Uh, yeah, maybe, I'm not sure I'm following you here...

- with an explanation for "rt" instead of "r"

Read the friggin' man page!

- returning error codes instead of using printfs

Up to the original poster to implement their error handling
routines; actually, I use something else in "real life" but modified
it for "readability"...

- with self-explanatory names e.g. process_file_by_line (insted of
assign_text_file_line_fields), process_line (instead of assign_func),
data or context (instead of assign_ptr)

Yeah, semantics, maybe...

"The best software is simple, elegant, and full of drama" - Grady Booch

Ah, brinksmanship, most applicable to nuclear power plant software...

---
William Ernest Reid



.



Relevant Pages

  • Re: Iterate before loop end
    ... >> I did consider GoTo, just for a moment, but you know what that leads ... >Yeah, in this instance, an optimal solution...no overhead of extra ...
    (microsoft.public.vb.general.discussion)
  • Re: Iterate before loop end
    ... > I did consider GoTo, just for a moment, but you know what that leads ... Yeah, in this instance, an optimal solution...no overhead of extra ...
    (microsoft.public.vb.general.discussion)
  • Re: OT well not...well yeah but not really
    ... OT well not...well yeah but not really ... its to do with tv shows from the 80's...sort of cult stuff... ... sort of thing, ok this gives away the idea... ... CHiPs drive fast on the highway now, goto ...
    (uk.culture.nostalgia.1980s)
  • Re: test
    ... Yeah, but you goto alt.test.test and no-one seems to listen... ... At the moment I am stranded in an ocean of Windows with no Linux in sight. ... matt -`;'- mbox1@alltel.net ...
    (comp.os.linux.setup)