• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Kent OlsenData 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
btocakciAuthor Commented:
i really thank you..Passing them into a dynamic array is a better idea i think..really thanks..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.