In an assignment I'm working on, I need to read data from a file and then convert that data into a form that can be stored in a structure. The structure stores information on the person the line in the file represents. The issue that I'm having is that I cannot find a way to build a function that will allow me to take this data and store it in the structure. This is the relevant code to getting the file:
FILE* ifp;
char file_name[150], early_type[5], name_holder[LENGTH];
double early_registration, indi_registration, team_registration, amount_holder;
int num_early_regist, age_holder, type_holder;
// get file name and set it up as a pointer
printf("Please enter the name of your file.\n");
scanf("%s", file_name);
ifp = fopen(file_name, "r");
The code then goes to scanning the file for the data:
fscanf(ifp, "%s %s %d %d %lf", early_type, name_holder, &age_holder, &type_holder, &amount_holder);
// if the line shows an individual registering early
if (strcmp(early_type, "INDV")==0){
person_early(struct person* person, &name_holder, &age_holder, &type_holder, &amount_holder);
}
Finally, this is the prototype of the function that I am building:
void person_early(struct person* person, char* name[LENGTH], int* age, int* event_type, double* donation_amount);
The issue that I'm having is that I'm not sure how to put the data into this structure:
struct person {
char name[LENGTH];
int number;
int age;
int event;
float money;
float time;
};
Should I build a function to store this data, as there are several people to log from the file? Do I need to be using pass by value or pass by reference? Thanks for any help.
Edit:
The full function that I have written is this:
void person_early(struct person* person, char* name[LENGTH], int* age, int* event_type, double* donation_amount) {
// the name of the variable should be the name that is taken from the file
struct person (*name[LENGTH]);
(*name).name = *name_holder;
(*name).age = *age_holder;
(*name).event = *type_holder;
(*name).money = *donation_amount;
return;
}
regarding:
(*name).name = *name_holder;
This is not what you want. The passed in struct pointer person
is the receiver of all the data. suggest:
void person_early( struct person* person, char name[], int age, int event_type, double donation_amount)
{
strcpy( person->name, name );
person->age = age;
person->event = event_type;
person->money = donation_amount;
}
Note: no return
needed on a void
function, unless exiting before the end of the function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.