Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading a parameter file into some data form

Status
Not open for further replies.

bocoer

IS-IT--Management
Jan 22, 2003
2
US
I am looking for a generic function for reading parameter
files and returning something like an array of strucures.
My param file is something like:
string string int float

The number of parameters is not fixed.

My problem is comes from returning a "list" of structures.

Thanks,
-r
 
Do a google search for linked lists and read the
man page on fscanf.

Simple idea:
Code:
int readnextRecord(FILE *fd, struct mytype *name) {
int p;
      if (name != NULL) {
         if ( (p = fscanf(fd,"%s %s %d %f",name->buf1,name->buf2,name->intnum,name->floatnum)) > 0) {
             return p;
         } else {
             return 0;
         }
      } else {
        printf("memory allocation error\n");
        exit(1);
      }
}

Or if you really want to return a struct take a look at
thread205-478093 and how the linked list is created.


HTH
 

My problem comes from the fact that I cannot write a routine
that will return an array of structures. I got the sscanf/fscanf thing down. I need something like:

array_of_structures?? ( char *file_to_read ) {

// 1) open file
// 2) read lines and fill struct
// 3) copy struct to an array?

// 4) return an_array_of structures??
// 5) close file
}

I need help with step 4 how to I append each structure to a
"list" of structures and then return that "list"
I hope you follow.
 
First:
Why do you need an array of structures?
A linked list will work just fine given
a decent method and a doubly linked list
if the list is large.

Second:
In any case you can create an array of
structures pseudo-dynamically or at runtime.
Code:
struct mytype **genStructArr(int n) {
int x;
struct mytype **mystructarr = malloc(n * sizeof(struct mytype *));
    
    for (x=0 ; x <= n ; x++) {
        mystructarr[x] = malloc(sizeof(struct mytype));
        if (!mystructarr[x]) {
            printf(&quot;malloc() failed\n&quot;);
            exit(1);
        }
    }
 
    if (mystructarr) {
       return mystructarr;
    }

return NULL;
}

It almost sounds like you are looking for a binary tree
of some sort, or possibly a hash table like structure??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top