[問題] c#刪除文字檔內的文字

作者: follow4ab (華洛)   2019-08-01 20:31:54
開發平台(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):
想請問版上的高手們應該如何修改呢?謝謝
作者: evil2004 (忘卻深淵)   2019-08-02 00:37:00
看不懂想表達什麼 ch是幹嘛的? 為何迴圈裡要ReadLine()?最後Console.Read()用意又何在? 為何要印出x?然後1.txt餵進去一定也是錯的 你確定你真的跑過?該不會最後的read是暫停吧?囧你在string line = sr.ReadLine();下面把下面這個印出來https://imgur.com/r9DaFGv
作者: s4300026 (s4300026)   2019-08-02 07:45:00
應該要學會用紅點點和眼鏡,這比較重要..
作者: Litfal (Litfal)   2019-08-02 10:55:00
What's the quiz's answer? I won't tell you
作者: Nilife (Al-Qaeda)   2019-08-14 17:51:00
Var ch 用意?

Links booklink

Contact Us: admin [ a t ] ucptt.com