-
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
}
}
}
-
So what exactly is your question?
Quzah.
-
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
-
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.
-
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