Is it safe to open an input stream to a file, while an output stream to the same file is open (but not writing to it)
(Single threaded)
Is it safe to open an input stream to a file, while an output
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Partially, it depends on the system. The system may not allow it. Other than that, as soon as you have a stream open for writing, and more than one stream open on the same file (regardless of direction), you have to worry about buffering. In your case, if you do a flush before opening the stream for reading, and do not write while you’re reading, it should be OK. If you’re trying to write, and read the last thing you wrote, it is more difficult, because there is nothing which you can do to resynchronize a read buffer with the file. And if you’re trying to write through two different streams, it’s even more problematic. (Although if the writes should always be appended at the end, and you can arrange when you flush, opening the files with
std::ios_base::appshould do the trick. But that won’t help reading.)