Home > Blockchain >  How do you make a counter using a text file in C?
How do you make a counter using a text file in C?

Time:06-19

I'm new at programming and I'm having a hard time making a permanent counter so that every time I terminate a program the program will still remember the count. I tried using files however I'm not quite sure whats happening

 printf("Entera number:\t");
 scanf("%d", &Num);

 sprintf(FILENAME, "counter.%d.txt", Num);
 FILE *counter;

 int Count, NewCount;

 counter = fopen(FILENAME, "w ");
 if (NULL != counter) {
 fseek(counter, 0, SEEK_END);
 int size = ftell(counter);

 if (0 == size) {
 fprintf(counter, "1");
 } 

 else if (size > 0) {

  rewind(counter);
  fscanf(counter, "%d", &Count);
  NewCount = Count   1;
  fprintf(counter, "%d", NewCount);

}
fclose(counter);

I wanna check if theres already a file created and it has a number stored inside it. if there is no file or there is no info inside the file I want it to store 1. If i open the file again, it will get the number in the file and add 1 to it and replace the 1 with 2 and so on.

CodePudding user response:

The following program:

  1. Checks if the file exists and has a number stored inside it.
  2. If yes, increments the number stored.
  3. Otherwise, ensures that the file exists and contains 1 by the time it finishes execution.
#include <stdio.h>

int main()
{
    int Count = 0;

    char FILENAME[] = "Counter.txt";
    FILE *counter = fopen(FILENAME, "a ");
    if (NULL != counter)
    {
        fseek(counter, 0, SEEK_END);
        int size = ftell(counter);

        if (size == 0)
        {
            printf("Number not found. Writing 1.\n");
            fprintf(counter, "1");
        }

        else if (size > 0)
        {
            rewind(counter);
            fscanf(counter, "%d", &Count);
            int NewCount = Count   1;
            fclose(counter);
            counter = fopen(FILENAME, "w");
            fprintf(counter, "%d", NewCount);
            printf("Incremented the existing number!\n");
        }
    }
    fclose(counter);
    return 0;
}

CodePudding user response:

One problem in your program is that opening the file in "w " mode will destroy all the file contents, if the file already exists. This behavior is desired after reading the counter, but not before reading the counter. Therefore, you should first open the file in "r" mode, read the counter, and then open it in "w" mode, destroying the previous file contents.

The following program will attempt to open a file named counter.txt and attempt to read the counter. If opening the file or reading the counter fails, then the counter will be set to 1. Otherwise, the counter will be incremented:

#include <stdio.h>
#include <stdlib.h>

#define FILENAME "counter.txt"

int main( void )
{
    FILE *fp;
    int counter;

    //attempt to open file and read counter
    fp = fopen( FILENAME, "r" );
    if ( fp == NULL )
    {
        counter = 1;
    }
    else
    {
        //read counter and increment it, on failure set it to 1
        if ( fscanf( fp, "%d", &counter ) != 1 )
        {
            counter = 1;
        }
        else
        {
            counter  = 1;
        }

        //cleanup
        fclose( fp );
    }

    //attempt to open file for output, overwriting all
    //file contents
    fp = fopen( FILENAME, "w" );
    if ( fp == NULL )
    {
        fprintf( stderr, "Error opening file!\n" );
        exit( EXIT_FAILURE );
    }

    //write updated counter to file
    fprintf( fp, "%d\n", counter );

    //cleanup
    fclose( fp );
}
  •  Tags:  
  • c
  • Related