Search code examples
c++filemode

Multiple Pointers To Same FILE With Different Access Mode C++


Is It possible to have multiple FILE * s point to the same file with different access modes? For example

lets say i had fopen("File1.bin","wb",fp1) and i perform write operations and WITHOUT closing the file using fclose i call fopen("File1.bin","rb",fp2) and try to use write operations on it. this should fail. but fp2 still writes content to it when i use a different access mode. Why?


Solution

  • fopen() opens a file stream, which is an abstraction of a file. Sure, a file handle is opened underneath but it is perfectly acceptable to have concurrent access to the same file through different handles (which may even be in different processes).

    A file is a shared resource.