1. C#如何讀取TXT最後一行數據
string fn = "test.txt";
StreamReader sr = new StreamReader(fn);
string c=sr.ReadToEnd();
string[] cs = c.Split(new char[]{'\n', '\r'});
sr.Close();
string lastLine= cs[cs.Length - 1];
lastLine即為最後一行,請注意如果Txt文件最後一行是穿行,則輸出lastLine時什麼也看不到!
2. C語言如何讀取txt文檔的最後一行數據
用fseek定位到文件末尾
並且逐字元讀取,讀一次就手動fseek到前一個字元,直到讀到回車換行就丟棄該字元並結束
然後將讀到的所有字元反序就是需要的最後一行數據了
3. c#中怎麼讀取txt文件的最後幾行
//使用StreamReader流打開txtStreamReader sr = new StreamReader(@"c:\dbsetting.txt");//讀取txt全部內容
string str = sr.ReadToEnd();//按\r\n分割為數組,數組的每一維就是一行數據
string[] aryStr = Regex.Split(str, "\r\n");//取最後一行string last = aryStr[aryStr.Length - 1];//倒數第二行就用aryStr.Length減2//關閉StreamReadersr.Close();