我有一个文件,其中包含希望监视更改的数据以及添加自己的更改。想像“尾巴-f foo.txt”。同时读写C#中的文件
基于this thread,看起来我应该创建一个文件流,并将它传递给作者和读者。但是,当读者到达原始文件的末尾时,它无法看到我自己写的更新。
我知道这似乎是一个奇怪的情况......它更多的是一个实验,看看它是否可以完成。
下面是示例情况下,我尝试:
foo.txt的:
一个
b
Ç
d
Ë
˚F
string test = "foo.txt";
System.IO.FileStream fs = new System.IO.FileStream(test, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
var sw = new System.IO.StreamWriter(fs);
var sr = new System.IO.StreamReader(fs);
var res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("g");
sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("h");
sw.Flush();
sw.WriteLine("i");
sw.Flush();
sw.WriteLine("j");
sw.Flush();
sw.WriteLine("k");
sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
经过“f”后,阅读器返回null。
一张海报确实提出了有关将两个文件流指向同一个对象的事情......这确实有效。即使读者到达文件末尾,如果作者更新,读者流也会得到两个结果。 – tbischel 2010-09-28 22:57:59
是的,那是我删除我的帖子后,它没有像我预期的那样工作。我解除了它的解释,为什么... – MarkPflug 2010-09-28 23:04:06