F
Franz Hose
Hi all!
I'm trying to read textual data files in C, but I'm still not sure what
the best design might be. All data files have this format
----sample data---
SOME_ID_IDENTIFYING_FILE_TYPE
data_record[0]
data_record[1]
....
data_record[n-1]
----end sample data---
somebody suggested the following strategy
typedef struct
{
/* status data, cur line number, etc. info */
FILE *f;
} FOO_FILE;
FOO_FILE *foo_open(char *filename)
{
/*
- alloc space for FOO_FILE
- open file (if that fails, return NULL or non-NULL and record
reason for failure?)
*/
}
int foo_close(FOO_FILE *)
{
/*
- close file if open
- free space for FOO_FILE
*/
}
int foo_readline(char *line, int len, FOO_FILE *foo)
{
/*
-fgets line (record possible errors inside *foo)
- if last char is not \n , error
- update line number
- if line is empty, repeat from start (???)
- return number of chars in line
*/
}
unsigned int foo_read_long(char *line, int len, FOO_FILE *foo)
{
/*
- foo_readline(/*...*/);
- if success, parse line as an int
- if not an int, store error condition in FOO_FILE struct (???)
- return this number
*/
}
, plus similar functions for reading lines with differently typed
data.
Finally, in the higher level code, all that needs to be done is
char line[LEN]
foo_open(/*...*/); /* check success */
foo_read_BAR1(/*...*/); /* check success */
foo_read_BAR2(/*...*/); /* check success */
while(foo_read_BAR3(/*...*/)
{
}
foo_close(/*...*/);
Does this sound sensible?
In particular, we would like all kinds of errors (file existance,
I/O errors, data format violations, etc.) to be recorded in a single
location, that is if error info is available in errno, pass it on to
the higher level functions, otherwise use some generic error code.
What would be the best place to pass error information around?
Are there better designs?
Thanks.
--
I'm trying to read textual data files in C, but I'm still not sure what
the best design might be. All data files have this format
----sample data---
SOME_ID_IDENTIFYING_FILE_TYPE
data_record[0]
data_record[1]
....
data_record[n-1]
----end sample data---
somebody suggested the following strategy
typedef struct
{
/* status data, cur line number, etc. info */
FILE *f;
} FOO_FILE;
FOO_FILE *foo_open(char *filename)
{
/*
- alloc space for FOO_FILE
- open file (if that fails, return NULL or non-NULL and record
reason for failure?)
*/
}
int foo_close(FOO_FILE *)
{
/*
- close file if open
- free space for FOO_FILE
*/
}
int foo_readline(char *line, int len, FOO_FILE *foo)
{
/*
-fgets line (record possible errors inside *foo)
- if last char is not \n , error
- update line number
- if line is empty, repeat from start (???)
- return number of chars in line
*/
}
unsigned int foo_read_long(char *line, int len, FOO_FILE *foo)
{
/*
- foo_readline(/*...*/);
- if success, parse line as an int
- if not an int, store error condition in FOO_FILE struct (???)
- return this number
*/
}
, plus similar functions for reading lines with differently typed
data.
Finally, in the higher level code, all that needs to be done is
char line[LEN]
foo_open(/*...*/); /* check success */
foo_read_BAR1(/*...*/); /* check success */
foo_read_BAR2(/*...*/); /* check success */
while(foo_read_BAR3(/*...*/)
{
}
foo_close(/*...*/);
Does this sound sensible?
In particular, we would like all kinds of errors (file existance,
I/O errors, data format violations, etc.) to be recorded in a single
location, that is if error info is available in errno, pass it on to
the higher level functions, otherwise use some generic error code.
What would be the best place to pass error information around?
Are there better designs?
Thanks.
--