diff --git a/lcthw/.ex17play.argv b/lcthw/.ex17play.argv new file mode 100644 index 0000000..4e9254b --- /dev/null +++ b/lcthw/.ex17play.argv @@ -0,0 +1 @@ +ex17play.bin diff --git a/lcthw/ex17play.c b/lcthw/ex17play.c new file mode 100644 index 0000000..b60a69d --- /dev/null +++ b/lcthw/ex17play.c @@ -0,0 +1,38 @@ +#include +#include +#include + +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; +}