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??
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);
}
}
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 :
Stream.Read
returns 0StreamReader.ReadLine
returns nullThere is no EOF character. Call FileStream.Read
in a loop. When .Read()
returns 0 for no bytes read, you're done.
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.