Skip to content

Instantly share code, notes, and snippets.

@JohannesMP
Last active November 7, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JohannesMP/95d3967b1db9942c4097c60335b85fab to your computer and use it in GitHub Desktop.
Save JohannesMP/95d3967b1db9942c4097c60335b85fab to your computer and use it in GitHub Desktop.
A ReadLine extension for FileStream objects
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