Re: Reading a table
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Tue, 27 Nov 2007 02:19:55 GMT
<Stephen.Schoenberger@xxxxxxxxx> wrote in message
news:91410ec6-e00d-4155-887c-ef8a12fc098a@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 26, 2:10 pm, user923005 <dcor...@xxxxxxxxx> wrote:
On Nov 26, 11:04 am, Stephen.Schoenber...@xxxxxxxxx wrote:
Hello,
My C is a bit rusty (.NET programmer normally but need to do this in
C) and I need to read in a text file that is setup as a table. The
general form of the file is
00000000 USNIST00Z 00000000_00 0 000 000 000 0000 000
I need to read the file line by line and eventually parse out each
piece of the file and store in arrays that correspond to the specific
line. array1[1] would be the first entry in the first line and so on
and so forth.
Any suggestions would be great!
fopen() to open the file.
fgets() to read in one line at a time.
write your own function to parse it, because only you know the format.
HTH
Thanks!
He forgot to tell you about fclose()!
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;
}
If you set up the above in its own little object file/library or
whatever, you can link it into any program when you need to
parse any type of text file with regularly-formatted lines of
column-data, by writing a specific assign_func for each type
of file, and declaring a suitable multi-dimensional array (or
array of structs) to hold the data:
typedef struct My_Data {
double field_1;
unsigned field_2;
int field_3;
} My_Data;
My_Data my_data_array[100];
My_Data *my_data_array_ptr=&my_data_array;
static unsigned
assign_my_file_fields
(unsigned line_num,char *text_line,void *data_ptr) {
My_Data *my_data_ptr=data_ptr;
my_data_ptr+=line_num;
/* line parsing and assigning goes here */
}
assign_text_file_line_fields
(text_file_path,assign_my_file_fields,(void *)(my_data_array_ptr));
And you'll never have to write the file opening, reading, and closing
code ever again...the only reason I bring this up is because, of course,
I have literally hundreds of different text file formats to read in my
own code...
---
William Ernest Reid
.
- Follow-Ups:
- Re: Reading a table
- From: Roland Pibinger
- Re: Reading a table
- References:
- Reading a table
- From: Stephen . Schoenberger
- Re: Reading a table
- From: user923005
- Re: Reading a table
- From: Stephen . Schoenberger
- Reading a table
- Prev by Date: Re: Precedence,Associativity and order of evaluation
- Next by Date: Re: Precedence,Associativity and order of evaluation
- Previous by thread: Re: Reading a table
- Next by thread: Re: Reading a table
- Index(es):
Relevant Pages
|