I have written a code to create a file and store 3 records each record has a string and a number.
My code successfully creates the file but i cannot read and display its contents.
Please tell me where i went wrong?
Code:
#include<stdio.h>
main()
{
FILE *fptr;
int n; //number declaration
char a[10]; //string declaration
fptr=fopen("myfile.txt","w"); //opening file
if(fptr==NULL) printf("Error opening file!"); //error
for(n=1;n<=3;n++) //loop for writing to file
{
gets(a);
fprintf(fptr,"%s %d\n",a,n);
}
fclose(fptr); //closing file
fptr=fopen("myfile.txt","r"); //opening in reading mode
while(fscanf(fptr,"%s %d\n",a,&n)!=EOF)
printf("%s %d\n",a,n); //display
fclose(fptr);
}