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

看板C_Sharp作者 (華洛)時間4年前 (2019/08/01 20:31), 編輯推噓3(306)
留言9則, 4人參與, 4年前最新討論串1/1
開發平台(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

08/02 00:37, 4年前 , 1F
看不懂想表達什麼 ch是幹嘛的? 為何迴圈裡要ReadLine()?
08/02 00:37, 1F

08/02 00:38, 4年前 , 2F
最後Console.Read()用意又何在? 為何要印出x?
08/02 00:38, 2F

08/02 00:39, 4年前 , 3F
然後1.txt餵進去一定也是錯的 你確定你真的跑過?
08/02 00:39, 3F

08/02 00:51, 4年前 , 4F
該不會最後的read是暫停吧?囧
08/02 00:51, 4F

08/02 00:57, 4年前 , 5F
你在string line = sr.ReadLine();下面把下面這個印出來
08/02 00:57, 5F

08/02 00:57, 4年前 , 6F

08/02 07:45, 4年前 , 7F
應該要學會用紅點點和眼鏡,這比較重要..
08/02 07:45, 7F

08/02 10:55, 4年前 , 8F
What's the quiz's answer? I won't tell you
08/02 10:55, 8F

08/14 17:51, 4年前 , 9F
Var ch 用意?
08/14 17:51, 9F
文章代碼(AID): #1TGjkyye (C_Sharp)