/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "score.dat"

void showall()
{
   int i,n,score[25];
   char line[100], name[25][100],*p;
   FILE *fp = fopen(FILENAME,"r");
   if(fp==NULL) return;
   n = 0;
   while(!feof(fp))
   {
      if(!fgets(line,sizeof(line),fp)) break;
      p = strrchr(line,' ');
      score[n] = atoi(p);
      *p = '\0';
      strcpy(name[n],line);
      n++;
   }
   fclose(fp);
   for(i=0;i<n;i++)
   {
      printf("%s %d\n",name[i],score[i]);
   }
}
void add(char *name,int score)
{
   FILE *fp = fopen(FILENAME,"a");
   fprintf(fp,"%s %d\n",name,score);
   fclose(fp);
}

int main()
{
   printf("1:\n");
   showall();
   add("a b c",100);
   printf("2:\n");
   showall();
   add("x y z",200);
   printf("3:\n");
   showall();
   add("nn",300);
   printf("4:\n");
   showall();
   return 0;
}
