Is it possible that application can lose I/O connection to a file?

Yes it's possible. For example if the file is on another machine or on a removable storage. Then you can physically disconnect/unplug the device and the subsequent operations on the handle will fail.

As others mentioned in the comments, you can simulate it in a test case by creating a RAM disk and either unmounting it or killing the driver. Alternatively, if all you care is about unidirectional I/O, maybe you could test your code with a pipe, and then close the other end of the pipe.


You can loose the connection with the file, but it won't set your file variable to NULL, the symptom will be an IO error. Reading or writing will fail. (For C++ streams, the badbit will be set instead of the failbit which is set for format error;)

Tags:

C++

C