/* counter-file.c -- print the counter value in a file. */ #include static void load_counter(); static void save_counter(); int counter = 0; main( int argc, char *argv[], char *envp[] ) { load_counter(); printf("%d -> ",counter ); counter = counter + 1 ; printf("%d\n",counter ); save_counter(); } #include /* pread() */ #include /* open() */ #include /* exit() */ #define FILENAME "counter.data" static void load_counter() { int fd ; fd = open(FILENAME,O_RDWR|O_CREAT,0666); if( fd < 0 ) { perror(FILENAME); exit(1); } read( fd, &counter, sizeof(counter) ); close( fd ); } static void save_counter() { int fd ; fd = open(FILENAME,O_RDWR|O_CREAT,0666); if( fd < 0 ) { perror(FILENAME); exit(1); } write( fd, &counter, sizeof(counter) ); close( fd ); }