Hello Guys, I just wanted to ask on how will I be able to print out the list of names in a data.txt file that I'm using..
My data.txt contains the Following
<Student Number> <First Name> <Last Name> <Age>
Code:
1 Chan Test 10
2 Test Chan 20
3 Chan Chan 30
4 Test Test 40
But the only one getting printed is
What I want to happen is like this
Code:
Chan Test
Test Chan
Chan Chan
Test Test
Code:
#include <conio.h>
#include <stdio.h>
main()
{
FILE *pFile;
char name[21],lname[21],answer;
int age,num,i;
pFile=fopen("data.txt","r");
fscanf(pFile,"%d",&num);
printf("List of names in the database");
for(i=0;i<num;i++)
{
fscanf(pFile,"%s %s",&name,&lname);
printf("\nName: %s %s",&name,&lname);
}
if(pFile!=NULL)
{
repeat:
printf("\n\nStudent Number: ");
scanf("%d",&num);
printf("First Name: ");
scanf("%s",&name);
printf("Last Name: ");
scanf("%s",&lname);
printf("Age: ");
scanf("%d",&age);
fprintf(pFile,"%d %s %s %d\n",num,name,lname,age);
{
fclose(pFile);
}
printf("Are there any more students? [Y/N]");
answer=getche();
if(answer=='Y'||answer=='y')
{
goto repeat;
}
else
{
printf("\nClosing Program");
}
}
else
{
printf("Could not open the file.\n");
}
getch();
}
Thank you in advance