typedef struct Data {
int name;
int surname;
int phone;
} Data;
char buf[50];
void main(int argc, char *argv[]) {
FILE *file;
file = fopen("src.txt", "r");
if (file == NULL) {
printf("The file is empty or doesnt exist.\n");
exit(-1);
}
fgets(buf, sizeof(buf), file);
Data dat[100];
int i;
while (!feof(file)) {
Data *d = dat + i;
sscanf(buf, "%d %d %d", &dat->name, &dat->surname, &dat->phone);
fgets(buf, sizeof(buf), file);
i++;
}
int n = i;
for (i = 0; i < n; i++) {
printf("Name: %d\nSurname: %d\nPhone: %d\n", dat[i].name, dat[i].surname,
dat[i].phone);
}
fclose(file);
exit(0);
}
I am trying to read lines of a file into a struct and then print the struct. the file looks like this:
Name Surname Phonenumber
Heinz Friedrich 015134532525
Amy Albertson 015443246678
When I try to print the struct it just shows me random numbers. I also tried to use chars but then it just says Segmentation fault (core dumped)
.