See
fseek - C++ Reference[
^]:
Quote:
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.
That means whenever you change the IO direction of a stream you have to call
fseek
. The reason is that there are two internal file positions: One for reading and one for writing but only the corresponding one is updated with a read or write.
fseek
will update both to the position of the last operation.
In your case the
fputc('$')
will update the write position but not the read position that will be still on the position of the ';' character. So that character will be read again with the next loop iteration resulting in an infinite loop. But the
fseek
call sets the read position to the current write position which is one behind.
BTW:
Do you think a buffer of 15 characters is sufficient to hold a full path (directory with file name)?
Use
PATH_MAX
instead which is defined in
limits.h.