Link to home
Start Free TrialLog in
Avatar of btocakci
btocakciFlag for Türkiye

asked on

reading files

Hi!i need your help..

i will read one of the lines of a file like

name   surname    age     sex     local .....(some more can be added)

and pass this words to a structure as arguments of it like students.name, students.surname...

but number of words can be change that means some more can be added or vice versa..

how can i pass them dynamically to a "students" structure?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi btocakci,

You don't really pass them to a structure.  Instead, you set the values in the structure and then pass the structure to other functions.

First, define your structure:

typedef struct
{
  char   Name[20];
  char   Surname[20];
  int      Age;
  char   Sex;
/*  Add more as you need them  */
}  MyStruct_t;

Then after reading the line, break it into pieces with sscanf(), strtok(), or your own method.  Then simply copy the pieces into the structure.


Good Luck!
Kent
Avatar of btocakci

ASKER

but i dont know how many arguments will the structure have..That is depend on the file..Let me say if the file says me to include a "school" argument (that means file can be edited) i must be able to use this "school" as a argument of my structure..
i think i need to alter my algortihm ha, that doesnot seem possible by this way?
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i really thank you..Passing them into a dynamic array is a better idea i think..really thanks..