Message: The file 'a.txt' is opened for read and write access at the same time on different streams
Category: Correctness
Severity: Warning
Language: C/C++
The same filename is opened for reading on one stream while another stream already has it open for writing (or vice versa).
Two independent streams onto the same file, one reading and one writing, can observe an inconsistent view of the file's contents depending on buffering and timing - a portability and correctness hazard that's easy to introduce without noticing, since each stream on its own looks fine.
Before:
#include <cstdio>
void f() {
FILE *f1 = fopen("a.txt", "w");
FILE *f2 = fopen("a.txt", "r"); // <- 'a.txt' is already open for writing
}After:
#include <cstdio>
void f() {
FILE *f1 = fopen("a.txt", "r");
if (f1) fclose(f1);
}- useClosedFile.md, readWriteOnlyFile.md,
writeReadOnlyFile.md, IOWithoutPositioning.md,
seekOnAppendedFile.md - other checks that follow the same
FILE*through a function.