i've been trying to write array into file and read them into the same program again, but the output is all wrong. i've googled, but i just can't find the solution. please help point out my mistakes. thank you.
Code:
#include<stdio.h>
int main()
{
FILE *fp;
char name[3][7];
int x;
fp=fopen("one.txt","w");
printf("\n Enter name:\n");
for(x=0;x<3;x++)
{
gets(name[x]);
fprintf(fp,"%s",name[x]);
}
fclose(fp);
fp=fopen("one.txt","r");
for(x=0;x<3;x++)
{
fscanf(fp,"%s",name[x]);
printf("\n Name[%d] entered: %s\n",x+1,name[x]);
}
fclose(fp);
system("pause");
return 0;
}
output:
Enter name:
lily
kiki
mimi
Name[1] entered: lilykikimimi
Name[2] entered: kiki
Name[3] entered: kiki
p/s: i learn from you tube videos only since my school doesn't cover the topic yet. please help.