none
How to read End of Line in C#

    Question

  • im working on a file and i wold like to be able to read the "end of line" and "end of file" i used to do that in C++ with EOF and EOL but i haven't found their equivalents in C#
    Wednesday, April 23, 2008 4:05 AM

Answers

  • First, with modern computers, if the file isn't huge, you can just read the whole thing at once.

    Use these very handy functions and you don't even need to create file streams:

    System.IO.File.ReadAllBytes(): takes a file path and gives you byte[]

    System.IO.File.ReadAllText(): takes a file path and gives you a string

    System.IO.File.ReadAllLines(): takes a file path and gives you string[]

     

    If you are sure you want to go reading smaller chunks of bytes, you just continue reading your FileStream until it's Read() function returns 0 (no more bytes to read). That's the end of the file.

     

    The EOF was inherited from very old times when every file had a EOF character at the end of it because they did not know the length of the file. Old programming languages continued to mimic that behaviour and give you an EOF flag when you reached end of file (determined by file length) even if there were no such character. Happily, C# has abandoned this relic.

    Wednesday, April 23, 2008 5:43 AM

All replies

  • Not sure what you are asking for here. The end of a line can (and does) vary depending on how the file was saved or where it was created. As for end of file, you can't read the end of the file, it's the end! Smile Do you want to read to the EOL/EOF?

     

     

    Wednesday, April 23, 2008 4:14 AM
  • First, with modern computers, if the file isn't huge, you can just read the whole thing at once.

    Use these very handy functions and you don't even need to create file streams:

    System.IO.File.ReadAllBytes(): takes a file path and gives you byte[]

    System.IO.File.ReadAllText(): takes a file path and gives you a string

    System.IO.File.ReadAllLines(): takes a file path and gives you string[]

     

    If you are sure you want to go reading smaller chunks of bytes, you just continue reading your FileStream until it's Read() function returns 0 (no more bytes to read). That's the end of the file.

     

    The EOF was inherited from very old times when every file had a EOF character at the end of it because they did not know the length of the file. Old programming languages continued to mimic that behaviour and give you an EOF flag when you reached end of file (determined by file length) even if there were no such character. Happily, C# has abandoned this relic.

    Wednesday, April 23, 2008 5:43 AM
  • Thanks, the return 0 works, now my program runs OK

     

    Wednesday, April 23, 2008 2:40 PM