beginner programmer here, I recently began learning the basics of c, I tried making a program that reads surnames and lastnames from a text document and stores them in structs and prints out the contents of the structs, however when executing I can't get any output, only random nonsense characters. What am I doing wrong?, I'm assuming that there is a problem with my usage of malloc() but I'm not sure.
The main file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define N 100
struct person {
char surname[N];
char lastname[N];
};
_Bool read_ord(char*surname, char*lastname, FILE* f){
char *temp =(char*) malloc(1000);
fgets(temp,N,f);
surname = strtok(temp," ");
lastname = strtok(NULL, " ");
if(surname != NULL && lastname != NULL){
return 1;
}
else
return 0;
}
int main (){
FILE *infil = fopen("personer.txt", "r");
struct person p[10];
char *surn;
char *lastn;
int a = 0;
while(read_ord(surn, lastn, infil) == 1){
strcpy(p[a].surname, surn);
strcpy(p[a].lastname, lastn);
printf("%s %s\n", p[a].surname, p[a].lastname);
a++;
}
}
personer.txt
Svensson Lars
Bolinder Annika
Kristersson Uffe
Andersson Lina
Stensdotter Erik