Populating a Struct

This is a discussion on Populating a Struct within the C Programming forums, part of the General Programming Boards category; Hello to all, Need help with this piece of my program. I'm trying to populate my struct with a data ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Populating a Struct

    Hello to all,
    Need help with this piece of my program. I'm trying to populate my struct with a data text file. Any suggestions

    Here is a piece of my program...
    Code:
    typedef struct
    {
            int grp_act_number;
            char *grp_name;
            float time_of_day;
            char day_of_week;
            short int week_of_month;
            int next_meeting;
            char bldg[4];
            char room_number[5];
            char purpose;
            int size;
            float cost;
    }STDT_GRP_ACT;
    
    int main (void)
    {
    
            //Declaration and Initialization of variables
            int h, i, j, k;
            int count = 0, index = 0, length = 0;
    
            STDT_GRP_ACT meeting;
            STDT_GRP_ACT building;
    
            STDT_GRP_ACT program[GROUPMAX];
    
            char tempbuffer[100];
    
            //Declares a file pointer called data, opens the text file, and reads
            FILE *data;
            data = fopen(" Lab2data.txt ", "r" );
    
           //Populate struct with with the file
            while( ( count >= 12 ) && ( count <= 20 ))      //Checks to make sure there are at least 12 and no more than 20 activity/groups
            {
                    for( i=0; i<GROUPMAX; i++ )
                    {
                            for( k=0; k<GROUPMAX; k++ )
                            {
                                    //copies groups number from file into the struct
    
    
                                    //copies groups name from file into the struct
                                    getline( tempbuffer, 99, data );
                                    length = strlen( tempbuffer );
                                            if( length >50 ) //Checks to see if it's too long
                                            {
                                                    tempbuffer[50] = '\0';
                                                    length = 50;
                                            }
                                            else     
                                            {
                                                    printf( "Only part of the name is accepted since it was too long \n" );
                                            }   
                                    program.grp_name = ( char *) malloc ( sizeof( char ) * length ); //Space allocated to name
                                    strcpy( program[i].grp_name, tempbuffer );      //Copies temp into program[i].grp_name
                            }
                    }
            }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what exactly is your question?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Basically, I need to know if I'm going down the right path... Like I have this text file that I read in. Now I need to take the data from the file and populate my struct that I had declared with it. I'm thinking I need to use a for loop to loop through the data file. fscanf

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Some sort of loop machanism is required to go through the datafile until you reach its end.
    The tricky part maybe reading and parsing input lines for assignment to struct members.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Actually you know that format of the data file which you are reading and after reading that just get the data from the file and populate the structure this is what can i say

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 06:38 PM
  2. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 08:13 AM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21