in reply to TIMTOWTDI - a counter
I haven't implemented flock() as I wasn't sure how portable it would be :-/#!/usr/bin/perl use Inline C; my $cnt = Counter("counter.txt"); die("there was an error incrementing the counter") if $cnt == -1; print "current count is $cnt\n"; exit(0); __END__ __C__ int Counter(char* filename) { FILE *fh; int count; char file_count[16]; if(NULL == (fh = fopen(filename, "r+")) ) return -1; if(NULL == fgets(file_count, 16, fh)) return -1; file_count[strlen(file_count) - 1] = '\0'; count = atoi(file_count); count++; if(-1 == fseek(fh, 0, SEEK_SET)) return -1; if(fprintf(fh, "%d\n", count) < 0) return -1; fclose(fh); return count; }
_________
broquaint
|
|---|