一个txt文件,有两行
aaaa
bbbb代码:
string s1 = @"D:\iosample\pp.txt";
using (StreamReader sr = new StreamReader(s1))
    {
       Console.WriteLine(sr.ReadLine());
    }输出结果为aaaa
我想问的是:
ReadLine方法的返回值是流中的下一行,而下一行是bbbb,那返回值应该是bbbb啊?应该输出bbbb吧MSDN

解决方案 »

  1.   

    StreamReader刚建立的时候, 当前位置是0,下一行就是第一行。using (StreamReader sr = new StreamReader(s1))
      {
      Console.WriteLine(sr.ReadLine());  //第一行
      Console.WriteLine(sr.ReadLine());  //第二行
      }ReadLine可以理解为一行一行读取。如果一上来就返回第二行,那第一行怎么办
      

  2.   

    你的msdn连接中不是有循环读取的代码吗
    using (StreamReader sr = new StreamReader(path)) 
                {
                    while (sr.Peek() >= 0) 
                    {
                        Console.WriteLine(sr.ReadLine());
                    }
                }
      

  3.   

    while (sr.Peek() >= 0) 
    你贴出来的例子,你不仔细看看吗?
      

  4.   

    using (StreamReader sr = new StreamReader(s1))
       {  while (!sr.EndOfStream)
                        {
       Console.WriteLine(sr.ReadLine());
    }
       }
                     
      

  5.   

    string path = @"D:\1.txt";//读取文件txt
                string sLine=string.Empty;
                using (StreamReader sr = new StreamReader(path))//遍历数据
                {
                    while (!sr.EndOfStream)//不是末尾数据
                    {
                        sLine = sr.ReadLine();
                        if (sLine.Length < 1)
                        {
                            continue;
                        }
                    }
                }
                  textBox1.Text = sLine.ToString();
    最后得到的是
    bbbb
      

  6.   

    阅读器初始时指向文件的开头,每执行一次ReadLine()读取一行,BOF:开头
    文件内容
    EOF:结尾开头、结尾没有任何行,可以认为阅读器初始时指向BOF。
      

  7.   

    引用百度百科:Before Of File
      Before of File,在电脑的术语缩写通常为 BOF,在作业系统决定资料源无更多的资料可读取。资料源通常称为档案或串流。   bof 用于指在第一条记录的前面,当记录集为空是,bof和eof都为true。
      

  8.   

    sr.Peek() >= 0 这个是关键,表示下行读出是否有值 如果有继续