16

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??
Péter Török
  • 114,404
  • 31
  • 268
  • 329
SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139

7 Answers7

19

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++;
     }
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    what would be the size of "b" (byte array) if FileStream's Read method returns 0 ? – Brij Feb 03 '14 at 17:17
  • Will b hold all the bytes read up until now or does it only hold the last iteration of read? the while loop confuses me. – DFSFOT Jan 07 '20 at 16:17
8

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);
    }
}
anijhaw
  • 8,954
  • 7
  • 35
  • 36
5

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.

erha
  • 59
  • 1
  • 1
5

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.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • @istepaniuk, the docs state "Read method returns zero only after reaching the end of the stream". http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx When have you seen other behavior? – Samuel Neff May 18 '14 at 19:08
  • @sammuel-neff You are right, I stand corrected, it will be at least one, never zero. Removed the down-vote; – istepaniuk May 18 '14 at 21:09
  • 1
    @istepaniuk, thanks for responding. Quoting the docs is better than just making the statement anyways, so I'm glad you gave me a good reason to edit my answer. – Samuel Neff May 19 '14 at 00:38
3

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
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
2

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.

Mattias S
  • 4,748
  • 2
  • 17
  • 18
-1

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();
            }
        }
    }
ItsAllABadJoke
  • 129
  • 2
  • 4