I want to read a txt file, line by line, and each line stores in a different variable: here is the txt file I want to read
Jenny
Woodbridge Ave
Amber
Exeter street
Michael
Main Street
David
Plainfield ave
and I did like
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct info
{
char name[20];
char add[50];
}INFO;
int main(void){
const char *fileName = "test.txt";
FILE *file = fopen(fileName, "r");
INFO* list = (INFO*)malloc(20*sizeof(INFO));
readFromFile(file,list);
fclose(file);
free(list);
return 0;
}
void readFromFile(FILE *file,INFO* list){
int i = 0;
while(!feof(file)){
fscanf(file,"%s %s\n ",(list+i)->name,(list+i)->adds);
i++;
}
}
but I getting
Name: Jenny
Addr: Woodbridge
------------------------------
Name: Ave
Addr: Amber
------------------------------
Name: Exeter
Addr: street
------------------------------
Name: Michael
Addr: Main
------------------------------
Name: Street
Addr: David
------------------------------
Name: Plainfield
Addr: ave
I just edited a little bit so I need to use fgets to read line by line instead of fscanf() right?