-3

Is it possible to count how many times that a program has been run in C? Do I need to setup a counter? For example:

#include <stdio.h>

int main()
{
    int var1;
    scanf("%d", &var1);

    int var2;
    scanf("%d", &var2);

    int var3;

    var3 =var1 + var2;

    printf("The answer is  = %d" , var3);

   /* if (The program ran for more than two times) {
        printf("The program is only allowed to run once or twice\n");
    }
    else {
      printf("The answer = %d" , var3);
    }*/

    return 0;
}

If the program had been run over two times, the program will display the error message once and restart to count.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
yoona1314
  • 33
  • 1
  • 8
  • Possible duplicate of [MATLAB's tic-toc & C's clock discrepancy](http://stackoverflow.com/questions/14636665/matlabs-tic-toc-cs-clock-discrepancy) – eyllanesc Jan 27 '17 at 02:28
  • 7
    If you want to count how many times your executable has been run, you would need to read from and write to a file or some other persistent memory (e.g. the windows registry if you are on a windows machine). – John Coleman Jan 27 '17 at 02:29
  • Clearly you would have to create a file to store the execution count in, read it when the program starts to see how many times it has run before, add one, and write it back out when exiting to update the stored value. You can search this site for `[c] write to file` (and also `read`) to see how to do that sort of thing. – Ken White Jan 27 '17 at 02:30
  • 2
    @eyllanesc I don't see the connection. Can you explain why you think that's a duplicate? –  Jan 27 '17 at 02:48
  • @eyllanesc: The3 question is unclear, but certainly not a dup of what you linked. – too honest for this site Jan 27 '17 at 03:06
  • Why do you need that? What is the actual use case? Why would you persist *only* the count of execution? What about several processes running the same program at the same time? Or the same program running on two computers on the same network? On what operating system? Did you consider using some database? You really need to **edit your question** and motivate it! – Basile Starynkevitch Jan 27 '17 at 05:52

1 Answers1

2

We can combine this answer with some simple file IO to achieve the desired effect.

Note that this is only an example of how to count the number of times a program has been run. Please do not use it in any security-sensitive context without careful consideration for the issues noted in the comment below this answer.

#include <unistd.h>
#include <sys/file.h>
#include <stdio.h>


int main(){
    int myCounterFd;
    if ((myCounterFd = open ("/tmp/myappname.counter", O_CREAT | O_RDWR, 0666)) < 0) {
        return -1;
    }
    if (flock (myCounterFd, LOCK_EX | LOCK_NB) < 0) {
        return -1;
    }
    FILE* fp = fdopen(myCounterFd, "w+");
    int count;
    size_t error;
    error = fread(&count, sizeof(count), 1, fp);

    printf("Error = %zu\n", error);
    // The program has never been run before
    if (error < 1) {
        count = 1;
        fwrite(&count, sizeof(count), 1, fp);
    } else{
        count++;
        rewind(fp);
        fwrite(&count, sizeof(count), 1, fp);
    }
    fflush(fp);
    printf("Program has now been run %d times.\n", count);

    flock (myCounterFd, LOCK_UN);
    close(myCounterFd);
}
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 2
    Setting the permissions to 666 is beastly, not least because it means anyone can reset the counter to zero trivially at any time. There's also an issue of permissions: while anyone who runs the program should be able to write to the file using the program, you don't want anyone not running the program to modify the file, except the owner. So, maybe you need a SUID program? That's more advanced stuff. The file with a known name in `/tmp` may not be a good idea either. The name has to be known, but …. Nit-picking — I know. – Jonathan Leffler Jan 27 '17 at 05:50
  • You are absolutely correct on all those points, I will add the caveat that this is meant only a an example of external persistence, not for production use. – merlin2011 Jan 27 '17 at 06:12