作者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://www.ptt.cc/bbs/C_Sharp/M.1564662716.A.F28.html
→ evil2004: 看不懂想表達什麼 ch是幹嘛的? 為何迴圈裡要ReadLine()? 08/02 00:37
→ evil2004: 最後Console.Read()用意又何在? 為何要印出x? 08/02 00:38
→ evil2004: 然後1.txt餵進去一定也是錯的 你確定你真的跑過? 08/02 00:39
→ evil2004: 該不會最後的read是暫停吧?囧 08/02 00:51
→ evil2004: 你在string line = sr.ReadLine();下面把下面這個印出來 08/02 00:57
推 s4300026: 應該要學會用紅點點和眼鏡,這比較重要.. 08/02 07:45
推 Litfal: What's the quiz's answer? I won't tell you 08/02 10:55
推 Nilife: Var ch 用意? 08/14 17:51