I want the result to be appear as
Here is your name: John
Here is your Date of birth: 02/02/1999
Although name, DOB are present in array and I am successfully fetching those values of name, dob etc. But the problem is that it is giving me this problem.
Here is your name: John
Here is your name: 02/02/1999
Here is your name: 442342352
Means with every value it is giving me only name, although everything is present in array.
#include <stdio.h>
#include <stdlib.h>
int main(){
const char listings[5][51] = {"Name", "Date of birth","ID card number","Phone number","Address"};
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
char ch[100];
int index = 0;
for (int i=0; i<5; i++){
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
if(ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your %s: %s\n",listings[i], ch);
index = 0;
}
else {
index++;
}
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
}
return 0;
}
forloop.forloop should be inside theif (fr != NULL)test, not the other way around.