Reading from file into multiple structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paulh
    New Member
    • Oct 2006
    • 1

    Reading from file into multiple structures

    Hi All,


    I am having problems reading from a file into an array of structures.
    I have defined an array of 100 structures as shown below.

    struct address_t

    {

    char name[name_size];

    int comp_one, comp_two, comp_three, comp_four ;

    } ;struct address_t address[100];


    I can fscanf into one structure no problems but cant work out how to scan into more than one. Here is the fscanf function that I am currently using-

    fscanf(InFile," %d.%d.%d.%d %s",&ap->comp_one, &ap->comp_two, &ap->comp_three, &ap->comp_four,&a p->name);

    ap is the pointer to the structure.

    I want to do something like this -

    fscanf(InFile," %d.%d.%d.%d %s",&ap->[i]comp_one, &ap->[i]comp_two, &ap->[i]comp_three, &ap->[i]comp_four,&ap->[i]name);

    and use a for loop with the counter (i) do that I can scan to the next structure out of the 100 possible.

    Any Ideas would be greatly appreciated,


    Thanks for your time,


    Paul.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by paulh
    I want to do something like this -

    fscanf(InFile," %d.%d.%d.%d %s",&ap->[i]comp_one, &ap->[i]comp_two, &ap->[i]comp_three, &ap->[i]comp_four,&ap->[i]name);

    and use a for loop with the counter (i) do that I can scan to the next structure out of the 100 possible.
    This is "want you want to do" and it would fix the problem. The only thing I can't work out is why you haven't done it?

    Comment

    Working...