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??

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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.

Method 4

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.

Method 5

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

Method 6

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.

Method 7

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();
            }
        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x