Re: [Code] User Counter

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 10/13/97


On Mon, 13 Oct 1997, Cris Jacobin wrote:

>         I'm having some trouble with the folloing function, and would
> appreciate some assistance with it.

>     fread(&u_cnt, sizeof (long), 1, uc_fp);

You have just opened the file. the above will read sizeof(long) bytes from
position 0.

>     fwrite(&u_cnt, sizeof (long), 1, uc_fp);

You then write those 4 bytes - to position 4-7 in the file.

Better than this, use a text file, and read in the count of players only
once, at bootup, and write it whenever it changes:

#define PLAYER_COUNT_FILE "../data/player_count"

int total_player_count;

void init_player_count() /* called at boot up */
{
        FILE *fp;
        if ((fp = fopen (PLAYER_COUNT_FILE, "r")))
        {
                total_player_count = fread_number(fp);
                fclose(fp);
        }
}

void increase_player_count() /* Called whenever a new player enters the game */
{
        FILE *fp;
        if ((fp = fopen (PLAYER_COUNT_FILE, "w")))
        {
                fprintf(fp, "%d\n", ++total_player_count);
                fclose(fp);
        }
        else
                bugf ("increase_player_count:%m");
}

I assume Circle has fread_number(). Your bug reporting procedue may vary:
the %m parameter means "insert string corresponding to current error
number here".

Using a text file you can see what the count is without doing hex
arithmethics, and it is portable across platforms too if you ever have to
move sites.

PS: The "b" crud in filemodes is only necessary if you want to be
compatible with DOS; it tells the stdio library not to write \n as \r\n
or something silly like that. Under UNIX, all files are binary.

 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://pip.dknet.dk/~erwin/>         <*>           (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST