作者follow4ab (华洛)
看板C_Sharp
标题[问题] c#删除文字档内的文字
时间Thu Aug 1 20:31:54 2019
开发平台(Platform):
(Ex: Win10, Linux, ...)
win10
编译器(Ex: GCC, clang, VC++...)+目标环境
(跟开发平台不同的话需列出)
viusal studio 内的c#
问题(Question):
换档案读取後就无法正常运作
喂入的资料(Input):
1.txt内容
1234567where is my friend? sorry 你没朋友
2.txt内容
7654321where do you live sorry i cannot tell you
预期的正确结果(Expected Output):
1.txt 1234567where is my friend
2.txt 7654321where do you live
错误结果(Wrong Output):
1.txt结果是成功的
2.txt结果 7654321 i tell you
程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档)
using (StreamReader sr = newStreamReader(@"C:\Users\Administrator\Desktop.1.tx
t"))
while (sr.Peek() != -1)
{ string line = sr.ReadLine();
foreach (var ch in line)
{ string[] words = line.Split();
string together = string.Join(" ", words); string x = sr.ReadLine();
if (x == "sorry")
{ break;
}
Console.WriteLine(x);
}
}
Console.Read();
补充说明(Supplement):
想请问版上的高手们应该如何修改呢?谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 42.74.47.23 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1564662716.A.F28.html
1F:→ evil2004: 看不懂想表达什麽 ch是干嘛的? 为何回圈里要ReadLine()? 08/02 00:37
2F:→ evil2004: 最後Console.Read()用意又何在? 为何要印出x? 08/02 00:38
3F:→ evil2004: 然後1.txt喂进去一定也是错的 你确定你真的跑过? 08/02 00:39
4F:→ evil2004: 该不会最後的read是暂停吧?囧 08/02 00:51
5F:→ evil2004: 你在string line = sr.ReadLine();下面把下面这个印出来 08/02 00:57
7F:推 s4300026: 应该要学会用红点点和眼镜,这比较重要.. 08/02 07:45
8F:推 Litfal: What's the quiz's answer? I won't tell you 08/02 10:55
9F:推 Nilife: Var ch 用意? 08/14 17:51