什么是字符文件流的文件尾? [英] what is character for end of file of filestream?

查看:187
本文介绍了什么是字符文件流的文件尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个特定字符while循环来检查它是否达到了文件的末尾。

哪一个角色,我可以搜索??

例如:

 的indexOf('/ N')行结束
的indexOf('')字结束
???? ----------文件结束?
 

解决方案

一流的末尾时,达到一个Stream.Read返回零。

从MSDN的例子,的FileStream

  //打开一个流,读回。
使用(的FileStream FS = File.OpenRead(路径))
{
    byte []的B =新的字节[1024];
    UTF8Encoding临时=新UTF8Encoding(真正的);
    而(fs.Read并(b,0,b.length个)大于0)
    {
        Console.WriteLine(temp.GetString(B));
    }
}
 

 使用(StreamReader的SR = File.OpenText(文件路径))
{
     串线;
     而((行= sr.ReadLine())!= NULL)
     {
          //做一些与行...
          lineCount ++;
     }
}
 

i am searching in a while loop for a particular character to check whether it reached the end of file.

Which character which i can search for ??

Eg:

Indexof('/n')  end of line
Indexof(' ') end of word
???? ---------- end of file??

解决方案

The end of a Stream is reached when a Stream.Read returns zero.

An example from MSDN, FileStream:

// Open a stream and read it back.
using (FileStream fs = File.OpenRead(path))
{
    byte[] b = new byte[1024];
    UTF8Encoding temp = new UTF8Encoding(true);
    while (fs.Read(b,0,b.Length) > 0)
    {
        Console.WriteLine(temp.GetString(b));
    }
}

or,

using (StreamReader sr = File.OpenText(filepath))
{
     string line;
     while ((line = sr.ReadLine()) != null)
     {
          // Do something with line...
          lineCount++;
     }
}

这篇关于什么是字符文件流的文件尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆