Write a program that prints how many times it has been run
by means of a runcount.txt
file like so:
$ ./run-count
run #1
$ ./run-count
run #2
$ ./run-count
run #3
If it is the first time your program is running in the working directory,
a runcount.txt
file should be created with the number 1
and your program should print run #1
.
Each time your program is run,
the number n in the runcount.txt
file should be incremented
and your program should print run #n
.
The number should be stored in the file in a proper line, i.e., followed by a line break.
To avoid issues with seeking the correct position on the runcount.txt
file,
you can open and close the file twice:
first for getting the current run count;
then for writing the updated run count.
This way you will not have to worry about
positioning the reading or writing pointers on the file
as whenever you reopen a file the pointer is positioned at the beginning.
Do not forget to follow the number in runcount.txt
by a line break.
try first: file-write file-read
Copyright © 2020-2023 Rudy Matela
All rights reserved