Count The Number Of Times A Program Runs

Code Id 2
Date Updated 3/7/2010
Title Count the number of times a program runs  
Description
Write a program that prints
   Hello
the first time it is run and
   Hello again
the next time it is run. 
                                  
Codes Snippet
#include 
#include 
#include 
#include 

void die(char *s)
{
  fprintf(stderr,"fatal: %s: %sn",s,strerror(errno));
  exit(111);
}

int main(void)
{
  FILE *f;

  f = fopen("alreadyran.dat","r");
  if (f) { printf("Hello againn"); exit(0); }

  if (errno != ENOENT) die("unable to read alreadyran.dat");

  printf("Hellon");
  f = fopen("alreadyran.dat","w");
  if (!f) die("unable to write alreadyran.dat");

  exit(0);
}

Comments are closed.