views:

75

answers:

5

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??
+4  A: 

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
Fast Finger First!
anijhaw
+4  A: 

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
+1. Also good...
Mitch Wheat
A: 

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
A: 

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

Sam
A: 

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