Thread: Counter problems

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Counter problems

    Hi all !

    My problem is that every time i open the program the number of recorods is multiplied. The records are normally saved in the file when the user exit the program or when the number of records is more than 20. I've got two static variables that suppose to control this process. One of them (static int cpt) is global to the program. The second (static int total) - is local and used as an anchor when the saving is actually takes place. Why on earth doesn't it work????????

    Here goes my saving function:
    /* APPEL - file name */
    /* appel - structure name */
    void ajout_nom_f()
    {

    static int total=0;/* pour compter le nombre d'enregistrements deja ecrits dans le nom_f */
    int i; /* compteur local */

    if((APPEL=fopen(nom_f,"a"))==NULL) /* ouverture nom_f dans le mode d'ajout - append */
    { err_msg("* * * Erreur du systeme! * * *\n");
    return;}
    for (i=total;i<cpt;i++)
    {
    fwrite(&tab_ap[i],sizeof(appel),1,APPEL); /*ajout du structure au nom_f*/
    }
    total = cpt;


    fclose(APPEL);
    return;
    }

  2. #2
    Registered User Italia's Avatar
    Join Date
    Feb 2002
    Posts
    13
    So, what you want to do is have a counter in a file? Just one number? Just fscanf the file for the number. Get the number, increment it, and then write it back to the file. If this isn't what you want to do, can you explain it better?

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    What I want to do..

    ..is to control the number of records.
    I don't want to copy the same number of records again and again. But that's obviously what's happening here.
    When the user exits the program, the records he added are saved in a file. When the user opens the program - the records from the file are copied into the RAM.

    So, say, we had 10 records in a file, then the user re-opened the program and added another 10. So when he closes the program down, 20 records are copied into the file instead of 10 new records...

  4. #4
    Registered User Italia's Avatar
    Join Date
    Feb 2002
    Posts
    13
    You'll just have to retreive the ten records into structs and then put them back in the file with the new ten records. Understand?

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    So, say, we had 10 records in a file, then the user re-opened the program and added another 10. So when he closes the program down, 20 records are copied into the file instead of 10 new records...
    I am not sure, I may not have understood this as this could be read both ways - but are you saying that you don't want the new records to be appended to the file? You want the new records to
    overwrite the previous records? If so:

    if((APPEL=fopen(nom_f,"w"))==NULL)

    You need to open the file for writing rather than appending. But of course it probably isn't that so the other thing I can suggest is to write some debug code that prints the value of "static int cpt". Since this variable is global - and since we can't see what is happening to it throughout the program in this code snippet.. it may be worth checking that the value of cpt is what you expect when you come to your for loop.

    Also you probably don't need that last return statement in the function.

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    static int cpt;

    Is this value ever reset to zero or some other number? It's a static global variable so every time you add some records to the file the value in cpt is going to increase - and when you come to your for loop in the save function it is going to hold it's previous value plus (presumably) the number of records entered.

    Now I can see that you are indexing the array by the number of records already entered into the file and counting up to the difference between this and the total number of records entered.

    But I think you will find that your problem is here somewhere, one of these variable probably doesn't hold what you expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  3. formulas & edit boxes, c++ on MSVC++ 5
    By TravelByMail in forum Windows Programming
    Replies: 1
    Last Post: 03-09-2002, 12:45 AM
  4. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM
  5. can you help me please?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2002, 09:21 AM