The suggestions so far have said:
- remove "&" in front of "string" variables in the fscanf() call.
- change "%d" to "%u" as the specifier for "unsigned int" variables in fscanf() call
- change "%s" to "%c" as the specifier for the "char" (not char [] ) variables in fscanf() call
- add length specifier to "%s" in fscanf() call
Note: you did not show the contents of "records.dat" file, which could not match the specifications.
And yet you say you've made the changes and still get a segmentation fault.
Lets assume this is your "records.dat" file (just one line):
<-my_name-> <-sir_name-> M <-email-> <-phone-> <-address-> <-levl-> 50000 5000 <-mon-unit-> <-currMODD-> 14.5 250
Here is an example of all of the above modifications (except #4) to your code, and which also prints out the results of fscanf(). It also contains your original code, commented out.
This does not segv when tested against my "records.dat". I leave it to you to integrate the #4 suggestion, and test it on your version of records.dat.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_FILE "records.dat"
struct record {
char name[64];
char surname[64];
char gender;
char email[32];
char phone_number[16];
char address[32];
char level_of_education[8];
unsigned int income_level;
unsigned int expenditure;
char currency_unit[16];
char currentModd[32];
float height;
unsigned int weight;
};
typedef struct record myrecord;
int main()
{
myrecord rItem;
FILE *fp;
struct record Myrecord;
if ((fp = fopen(DATA_FILE,"r")) !=NULL)
{
/* ORIG: fscanf(fp,"%s %s %s %s %s %s %s %d %d %s %s %f %u\n",
&rItem.name, // 1
&rItem.surname, // 2
&rItem.gender, // 3 - char
&rItem.email, // 4
&rItem.phone_number, // 5
&rItem.address, // 6
&rItem.level_of_education, // 7
&rItem.income_level, // 8 - unsigned int
&rItem.expenditure, // 9 - unsigned int
&rItem.currency_unit, // 10
&rItem.currentModd, // 11
&rItem.height, // 12 - float
&rItem.weight); // 13 - unsigned int
*/
// 1 2 3 4 5 6 7 8 9 10 11 12 13
fscanf(fp,"%s %s %c %s %s %s %s %u %u %s %s %f %u\n",
rItem.name, // 1
rItem.surname, // 2
&rItem.gender, // 3 - char
rItem.email, // 4
rItem.phone_number, // 5
rItem.address, // 6
rItem.level_of_education, // 7
&rItem.income_level, // 8 - unsigned int
&rItem.expenditure, // 9 - unsigned int
rItem.currency_unit, // 10
rItem.currentModd, // 11
&rItem.height, // 12 - float
&rItem.weight); // 13 - unsigned int
printf("doneeee\n");
printf("name: %s\n"
"surname: %s\n"
"gender: %c\n"
"email: %s\n"
"phone: %s\n"
"address: %s\n"
"ed_level: %s\n"
"incm_lvl: %u\n"
"expend: %u\n"
"CurrUnit: %s\n"
"CurrModd: %s\n"
"height: %f\n"
"weight: %u\n",
rItem.name, // 1
rItem.surname, // 2
rItem.gender, // 3 - char
rItem.email, // 4
rItem.phone_number, // 5
rItem.address, // 6
rItem.level_of_education, // 7
rItem.income_level, // 8 - unsigned int
rItem.expenditure, // 9 - unsigned int
rItem.currency_unit, // 10
rItem.currentModd, // 11
rItem.height, // 12 - float
rItem.weight); // 13 - unsigned int
}
else{
printf("errorrrr ");
}
}