reading files

Posted on 2006-04-26
Last Modified: 2010-04-15
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.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?
Question by:btocakci
    LVL 45

    Expert Comment

    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!

    Author Comment

    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..

    Author Comment

    i think i need to alter my algortihm ha, that doesnot seem possible by this way?
    LVL 45

    Accepted Solution

    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.


    Author Comment

    i really thank you..Passing them into a dynamic array is a better idea i think..really thanks..

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now