I have a StreamWriter
open to my file during the same time that I read from it, which seems to be causing issues (this is a smaller snippet of a larger set of code, just shown to illustrate my issue):
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
using (FileStream stream = new FileStream("file.txt", FileMode.OpenOrCreate))
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, false, 0x1000, true))
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 0x1000, true))
{
Console.WriteLine("Read \"" + reader.ReadToEnd() + "\" from the file.");
}
}
Console.ReadLine();
}
The above code will output:
Read "" from the file.
Read "" from the file.
Read "?" from the file.
If the file already contains some text, the writer will append the BOM to the end despite never having been called to write anything:
Read "TEXT" from the file.
Read "TEXT?" from the file.
Read "TEXT??" from the file.
Why does it exhibit this behavior?