p4est_save_ext
fails on Windows due to different behavior of fopen
/ftell
#113
Labels
p4est_save_ext
fails on Windows due to different behavior of fopen
/ftell
#113
Description
The function
p4est_save_ext
fails under Windows at the following line:p4est/src/p4est.c
Line 3527 in 3a78fd2
A similar issue occurs in
p6est_save_ext
atp4est/src/p6est.c
Line 761 in 3a78fd2
I manually instrumented the code at the problematic locations to show the return value of
ftell
, which gives the number of bytes from the beginning of the file to the file position indicator (see, e.g., here). The return value is indeed0
, indicating an empty file (an actual error would have been indicated by a return value of-1
). However, by looking at the output file generated until this point, it is clear that the file is indeed non-empty. So what happened?It took me a while to figure out the reason for this, but it is related due to the different behavior of
ftell
on Windows vs. Linux/macOS:A few lines before the line in which the error occurs, the output file is opened in "(binary) append" mode using
fopen(filename, "ab")
:p4est/src/p4est.c
Lines 3521 to 3527 in 3a78fd2
According to most sources documenting the C function
fopen
(e.g., here or here), opening a file in "append" mode seems to mean that the file position indicator is set to the end of the (possibly existing) file to ensure that new data is written at the end. This behavior is used inp4est.c#L3527
andp6est.c#L761
to ensure that the previous write operation has succeeded by asserting the return value offtell
to be greater than zero.However, on Windows the behavior of
fopen
/ftell
differs in a small, but in this case significant, detail (source):and
This causes the checks in
p4est.c#L3527
andp6est.c#L761
to fail, since the return value offtell
after opening is0
, even though the file has been created and written to correctly before.To Reproduce
An MWE to produce this error is, e.g., as follows:
Compiling and running this under Windows yields the following output:
Additional information
More information on how to reproduce the erroneous behavior can be found at https://github.com/sloede/p4est-demo. It was reproduced under Windows using a current MSYS2 installation and the
mingw-w64-x86_64-toolchain
package.The text was updated successfully, but these errors were encountered: