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?
btocakciAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi btocakci,

You can always build a dynamic array that is really a pointer to an array, but pointer to pointer logic is fairly obtuse.  You will crash your program several times trying to get a grasp on it.  And unless that is your specific assignment, I'd suggest that another way must be found.

Your structure will have to match the data file.  That's pretty common in the world.  I would suggest that you write the program and structure based on a known file.  Then if you need to expand the structure to work with a different file you simply edit the program and structure to handle the new format.



Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
 
btocakciAuthor Commented:
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..
0
 
btocakciAuthor Commented:
i think i need to alter my algortihm ha, that doesnot seem possible by this way?
0
 
btocakciAuthor Commented:
i really thank you..Passing them into a dynamic array is a better idea i think..really thanks..
0
All Courses

From novice to tech pro — start learning today.