10 thousands dollars question about file
Hi,
If anyone can answer to that, he will deserve my most sincere admiration. In this code (french by the way, but I translated the printf content) , there is a problem with the output (related to the end of file). This program read names in a .txt file, ask you to modify one and then, he create a new .txt with the modified name. The problem is that the last name come two time
ex:
name.txt
John
Gill
modify John for Toto
the the output file is
newname.txt
Toto
Gill
Gill
why 2 Gill, and how can you fix that?
if you can solve this, you will beat a teacher I know that can't solve it.
Code:
#include <stdio.h>
#include <string.h>
#include <myconio.h>
void OuvrirFichier(char szNomPhys[40], FILE * * ref_fich, char szTypeAcces[3]);
int main(void)
{
char szAncien[30], szNouveau[30];
FILE *fEntree, *fSortie;
char szNom[30], szNomModif[30], szNomNouveau[30];
printf("Name of the old file (.txt) : ");
scanf("%s", szAncien);
OuvrirFichier(szAncien,&fEntree,"r");
printf("Name of the new file : ");
scanf("%s", szNouveau);
OuvrirFichier(szNouveau,&fSortie,"w");
printf("name to modify : ");
scanf("%s",szNomModif);
printf("new name : ");
scanf("%s",szNomNouveau);
while (!feof(fEntree))
{
fscanf(fEntree, "%s", szNom);
if (strcmp(szNom, szNomModif) == 0)
fprintf(fSortie, "%s\n", szNomNouveau);
else
fprintf(fSortie, "%s\n", szNom);
}
fclose(fSortie);
fclose(fEntree);
}
void OuvrirFichier(char szNomPhys[40], FILE * * ref_fich, char szTypeAcces[3])
{
if (( *ref_fich = fopen(szNomPhys,szTypeAcces)) == NULL)
{
printf("Erreur: %s",szNomPhys);
exit(1);
}
}
thanks