Could someone point out why my exit character 'x' is being written to my sequential file? I would really appreciate it. Thank You!
Code:
#include <stdio.h>
#include <stdlib.h>
#define N 100
FILE *fP;
main()
{
char str[N];
int i;
fP = fopen("names.dat", "w");
if ((fP = fopen("names.dat", "w")) == NULL)
{
printf("%s not opened", "names.dat");
exit(EXIT_FAILURE);
}
printf("This program will write names and GPAs into a file named names\n");
printf("Please enter an x to exit the program.\n");
for(i=0; i<N; i++)
{
while (i < N && *str != 'x')
{
printf("Please enter a student's name and their GPA separted by a space.\n");
gets(str);
fputs(str, fP);
fputs("\n", fP);
fflush(stdin);
}
}
fclose(fP);
return 0;
}
Also if you see any improvements I can make, please feel free to point it out. With that being said, please be kind.