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();