Last active
November 7, 2019 12:40
-
-
Save JohannesMP/95d3967b1db9942c4097c60335b85fab to your computer and use it in GitHub Desktop.
A ReadLine extension for FileStream objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Collections.Generic; | |
public static class FileStreamReadLineExtension | |
{ | |
public static string ReadLine(this FileStream fs) | |
{ | |
int curChar; | |
// EOF - return null | |
if ((curChar = fs.ReadByte()) == -1) | |
return null; | |
// Return a string | |
List<char> chars = new List<char>(); | |
do | |
{ | |
// End of Line | |
if (curChar == '\n' || curChar == '\r') | |
break; | |
chars.Add((char)curChar); | |
} | |
while ((curChar = fs.ReadByte()) != -1); | |
// Make sure we also skip past any \r\n | |
if (curChar == '\r' && fs.ReadByte() != '\n') | |
fs.Seek(-1, SeekOrigin.Current); | |
// Everything we read this line | |
return new string(chars.ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment