You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
box-o-sand/lcthw/ex17play.c

39 lines
579 B

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2) {
errx(EXIT_FAILURE, "Missing filename argument\n");
}
int init = 0;
FILE *fp = fopen(argv[1], "r+");
if (!fp) {
fp = fopen(argv[1], "w+");
init = 1;
}
if (!fp) {
err(EXIT_FAILURE, "Failed to open file\n");
}
int counter = 0;
if (!init) {
fread(&counter, sizeof(int), 1, fp);
}
counter++;
rewind(fp);
size_t wrote = fwrite(&counter, sizeof(int), 1, fp);
fprintf(stderr, "Wrote %d (%lu bytes)\n", counter, wrote);
fclose(fp);
return 0;
}