I want to implement a function that gets as parameter a FILE*
, that is already open in write mode (a
, a+
, w
, w+
etc), and reads the contents of that file. The problem here is that the file is already locked (because of the fopen
) and I need to close it before starting reading.
However, after I have read the file I need to reopen it just like it was before my function was called.
Thus, I need both the file name and the mode the fopen
was called with, initially.
I was able to get the file name. However the mode seems to be tricky. Since now I was able to get the mode code using the code below.
int fd = fileno(file_pointer);
int mode = fcntl(fd, F_GETFL);
Unfortunately, there is inconsistency among systems (same open mode -> different mode codes). Is the method shown above an actual way to get the mode a file was open in? Do you have any other ideas?