Thread: 10 thousands dollars question about file

  1. #1
    Registered User
    Join Date
    Mar 2007

    10 thousands dollars question about file

    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


    modify John for Toto

    the the output file is

    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.
    #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("&#37;s", szAncien);
       printf("Name of the new file : ");
       scanf("%s", szNouveau);
       printf("name to modify : ");
       printf("new name : ");
       while (!feof(fEntree))
          fscanf(fEntree, "%s", szNom);
          if (strcmp(szNom, szNomModif) == 0)
             fprintf(fSortie, "%s\n", szNomNouveau);
             fprintf(fSortie, "%s\n", szNom);
    void OuvrirFichier(char szNomPhys[40], FILE * * ref_fich, char szTypeAcces[3])
        if (( *ref_fich = fopen(szNomPhys,szTypeAcces)) == NULL)
           printf("Erreur: %s",szNomPhys);

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    It's like this... your reading in and writing out "Gill" once... your loop condition isn't meeting, you're next read is failing leaving Gill in your szNom buffer which is then getting printed to the file, again. There are several ways you can avoid this and I imagine you're up to the challenge of figuring it out on your own.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Here is your problem:
    while (!feof(fEntree))
    See this FAQ for why not to do that:

    What happens is that your program works perfectly until it comes to the last line. Then the fscanf() discovers this and returns an error code (which you ignore). It leaves the string untouched. Then you process the string as normal, and only afterwards do you discover (with feof()) that the end of the file was reached.

    There are several ways to fix this. The best in my opinion is to check the return value of scanf():
    while(fscanf(fEntree, "&#37;s", szNom) == 1)) {
        /* ... */
    (*scanf() returns the number of items successfully read. Since you passed it one item, %s, it will return 1 on success. It could return 0 or EOF on failure.)

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    Where do schools get these teachers? And nevrax, you keep asking questions that are addressed in the FAQ. It's there for a reason, buddy.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Seriously, I didn't though that this one would be in a faq, it's an "experienced teacher" in C that didn't found the mistake, I wouldn't have given such a title if I knew it was such a little problem. Well, guess we don't have the teacher we once had...or the problem is that these teacher learned Cobol, Fortrean and Dbase and they never updated. And for the faqs, I kind of have some difficulty reading them ( English is not my primary language), I don't even know if what I write is good. The only way for me is to ask, or read French faqs...but they suck most of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM