10 thousands dollars question about file

This is a discussion on 10 thousands dollars question about file within the C Programming forums, part of the General Programming Boards category; Hi, If anyone can answer to that, he will deserve my most sincere admiration. In this code (french by the ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    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("&#37;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

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    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
    Location
    Canada
    Posts
    8,046
    Here is your problem:
    Code:
    while (!feof(fEntree))
    See this FAQ for why not to do that: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    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():
    Code:
    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.)
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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
    Posts
    42
    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, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21