I trying to open a simple txt file in C, like the image bellow.
The input text :
Name Sex Age Dad Mom
Gabriel M 58 George Claire
Louise F 44
Pablo M 19 David Maria
My doubt is, how can i make to identify the blank spaces in the list and jump correctly to another lines.
Here is my code:
#include <stdio.h>
int main() {
FILE *cfPtr;
if ((cfPtr = fopen("clients.txt", "r")) == NULL) {
puts("The file can't be open");
} else {
char name[20];
char sex[4];
int age;
char dad[20];
char mom[20];
char line[300];
printf("%-10s%-10s%-10s%-10s%-10s\n","Name","Sex","Age","Dad","Mom");
fgets(line,300,cfPtr);
fscanf(cfPtr,"%10s%10s%d%12s%12s",name,sex,&age,dad,mom);
while (!feof(cfPtr)) {
printf("%-10s%-10s%d%12s%12s\n",name,sex,age,dad,mom);
fscanf(cfPtr,"%19s%3s%d%12s%12s",name,sex,&age,dad,mom);
}
fclose(cfPtr);
}
return 0;
}
It works fine if I fill in all the spaces...
fscanf()
instead offeof(cfPtr)
. Who or what text suggestedwhile (!feof(cfPtr)) {
? - chux - Reinstate Monicawhile (!feof(cfPtr))
was an example from my teacher, i guess it wasnt a good one. I will try use the return value of fscanf. - noodles1211