double registration

This is a discussion on double registration within the C Programming forums, part of the General Programming Boards category; Hy, i have some problems with a code.. When i choose to display the content of a file, the last ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    Question double registration

    Hy, i have some problems with a code.. When i choose to display the content of a file, the last registration is displayed twice.

    http://img121.imageshack.us/img121/336/afisare.jpg

    This is the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    
    typedef struct
    {
        unsigned int id;
        char nume[30], model[50], carburant[10];
        int pret, km, putere, an_fab;
        int is; //indicator stare
    } masini;
    
    
    /*************/
    /*CREATE FILE**/
    /*************/
    
     void creare_fisier()
     {
         char fis[20];
         FILE* f;
         int opt_1;
         masini x;
         unsigned int id=1;
         printf("\n > Nume fisier: ");
         scanf("%s",&fis);
         f=fopen(fis,"r+b");
         if (f)
         {
             printf("\nFisier deja existent!");fclose(f);Sleep(1500);
             printf("\n\n------------------------------------");
             printf("\n0. Intoarcere in meniul principal");
             printf("\n1. Stergere");
             printf("\n------------------------------------");
             printf("\n\n > Alegeti optiunea: ");
             fflush(stdin);
             scanf("%d",&opt_1);
             if(opt_1==1)
             {
                 remove(fis);
                 printf("\nFisierul \"%s\" a fost sters cu succes!",fis);
                 Sleep(2000);
             }
         }
         else
         {
             f=fopen(fis,"w+b");
             printf("\n > Nume marca masina cod %u sau Ctrl+Z pt salvare BD: ",id);
             fflush(stdin);
             gets(x.nume);
             while (!feof(stdin))
             {
    			 x.is=1;
                 x.id=id;
                 printf("\n > Modelul masinii: ");
                 fflush(stdin);
                 gets(x.model);
                 printf("\n > Carburant: ");
                 fflush(stdin);
                 gets(x.carburant);
                 printf("\n > Pret [EURO]: ");
                 scanf("%d",&x.pret);
                 printf("\n > Kilometraj: ");
                 scanf("%d",&x.km);
                 printf("\n > Putere [CP]: ");
                 scanf("%d",&x.putere);
                 printf("\n > Anul fabricatiei: ");
                 fflush(stdin);
                 scanf("%d",&x.an_fab);
                 fseek(f,(id-1)*sizeof(masini),0);
                 fwrite(&x,sizeof(masini),1,f);
                 id++;
                 printf("\n > Numele urmatoarei masini cod %u sau Ctrl+Z pt salvare BD: ",id);
                 fflush(stdin);
                 gets(x.nume);
    		 }
             fclose(f);
         }
     }
    
    
    int nr_art(FILE *f, int l)
     {
         int poz=ftell(f);
         fseek(f,0,2);
         int c=ftell(f)/l;
         fseek(f,poz,0);
         return c;
     }
    
    
    /******************************/
    /*DISPLAY THE CONTENT OF THE FILE*/
    /******************************/
    
     void afisare()
     {
         FILE *f;
         masini x;
         int i;
         char fis[20];
         printf("\n > Numele fisierului sursa: ");
         scanf("%s",&fis);
         f=fopen(fis,"rb") ;
         if(f==0)
         {
             printf("\nEroare! Nu exista fisierul cerut.");
             Sleep(2000);
         }
         else
         {
             fseek(f,0,0);
             fread(&x, sizeof(masini),1,f);
             for(int i=0;i<=nr_art(f,sizeof(masini));i++)
             if(x.is==1)
             {
                 printf("\n\n > Codul masinii: %u", x.id);
                 printf("\n > Numele marcii masinii: %s", x.nume);
                 printf("\n > Modelul masinii: %s", x.model);
                 printf("\n > Carburant: %s", x.carburant);
                 printf("\n > Pretul: %u EURO", x.pret);
                 printf("\n > Kilometraj: %u Km", x.km);
                 printf("\n > Putere: %u CP", x.putere);
                 printf("\n > Anul fabricatiei: %u", x.an_fab);
                 fread(&x,sizeof(masini),1,f);
             }
             else fread(&x,sizeof(masini),1,f);Sleep(2000);
             fclose(f);
         }
     }
    
    
    /******/
    /*MAIN */
    /******/
    
     int main(void)
     {
         int optiune;
         while(optiune)
         {
             cuprins();
             scanf("%d",&optiune);
             switch(optiune)
             {
                 case 1 : creare_fisier();     break;
                 case 2 : adaugare();          break;
                 case 3 : stergere();          break;
                 case 4 : afisare();           break;
                 case 5 : actualizare_pret();  break;
                 case 6 : selectare();         break;
                 case 0 : printf("\n Multumesc pentru testarea programului!\n"); exit(1);
                 default: {printf("\nOptiune inexistenta!"); Sleep(1500); break;}
             }
         }
         getch();
     }
    Last edited by 1qLuke; 01-13-2010 at 08:23 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    Plus you're not using the return result of fread() etc to work out when the end of the file is reached.
    feof() does NOT do what you think it should.
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions, have errors...NEED HELP FAST
    By alkamenes in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 02:00 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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