开发者

what is character for end of file of filestream?

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++;
     }
}


Maybe what you are looking for is this

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    String line;
    while ((line = sr.ReadLine()) != null)
    {
         Console.WriteLine(line);
    }
}


When FileStream return 0, it doesn't mean that you have reach end of file. I had experience that.

From MSDN: The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.

This happen on slow device like thumbdrive.


There is no EOF character. Call FileStream.Read in a loop. When .Read() returns 0 for no bytes read, you're done.

The docs are very clear on this behavior.

http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx

The Read method returns zero only after reaching the end of the stream. Otherwise, Read always reads at least one byte from the stream before returning. If no data is available from the stream upon a call to Read, the method will block until at least one byte of data can be returned. An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.


There is no "end of file character" in a string (or even in a file). The length of the string is known (Length property), so it's not necessary

When reading a file, you can check :

  • if Stream.Read returns 0
  • if StreamReader.ReadLine returns null


There's no such character. If you call FileStream.ReadByte, it will return -1 for end-of-file. The Read method return zero bytes read. If you use a StreamReader around the stream, its ReadLine method returns null or its EndOfStream property returns true.


Sometimes you don't want to read the whole line. For example, if the line is very long, or saving the string in a temporary variable isn't useful.

In these cases, you can use the Peek() function on the StreamReader. When it returns -1 you are at the end. For example:

    // Reads a CSV file and prints it out line by line
    public static void ReadAndPrintCSV(string fullyQualifiedPath)
    {
        using (System.IO.StreamReader sr = File.OpenText(fullyQualifiedPath))
        {
            string[] lineArray = null;
            while ((sr.Peek() > -1) && (lineArray = sr.ReadLine().Split(',')) != null)
            {
                foreach (string str in lineArray)
                {
                    Console.Write(str + " ");
                }
                Console.WriteLine();
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜