什么是字符文件流的文件尾?文件、字符

2023-09-04 07:50:31 作者:青丝

我在寻找一个特定字符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++;
     }
}

 
精彩推荐
图片推荐