Re: Reading a table




<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



.



Relevant Pages

  • Re: problem with macros
    ... array instead of x, y, z. ... some macros that could help me in reducing the triplification of code ... macros ...
    (comp.lang.c)
  • Re: CreateMapFile e LPCWSTR
    ... An alternative is to calculate the size of each string and then allocate enough memory for those strings + 2-4 bytes for a counter preceding each string. ... each string and placing them in an array, ...
    (microsoft.public.pocketpc.developer)
  • Re: Passing an array of structures from a pointer [3]
    ... typedef struct ddlbColl1{ ... int LIST_NUMBER; ... DDLB *obj_DDLB; ... Use additional braces for every initialization list for an array element ...
    (microsoft.public.vc.language)
  • Re: problem with macros
    ... I could avoid triplification of code by using an ... array instead of x, y, z. ... (physics people). ...
    (comp.lang.c)
  • Re: Need a GURU!!! Im stuck.
    ... array of ELEMENT_T have an ELEMENT_T * in the outer struct. ... typedef struct table_entry ... This costs an additional pointer per entry in table, ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)