Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??
like image 922
SmartestVEGA Avatar asked Mar 11 '10 14:03

SmartestVEGA


People also ask

What char is EOF?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

What is end of file in C#?

Use EOF to avoid the error generated by attempting to get input past the end of a file. The EOF function returns False until the end of the file has been reached. With files opened for Random or Binary access, EOF returns False until the last executed FileGet function is unable to read a whole record.

What is the difference between FileStream and StreamWriter?

Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes. A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text.


1 Answers

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++;
     }
}
like image 183
Mitch Wheat Avatar answered Sep 23 '22 17:09

Mitch Wheat