/ Published in: C#
Expand |
Embed | Plain Text
namespace CSConsole { public class Program { public static void Main() { //calling the function for asccii file with 500 bytes of size ReadBinaryFile("C:\\Temp\\sth.txt", Encoding.ASCII, 500); } private static void ReadBinaryFile(string path, Encoding encoding, int bufferSize) { try { string line, whole, halfLine = string.Empty; using (FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read)) { while (fileStream.Position < fileStream.Length) { int len = fileStream.Read(buffer, 0, buffer.Length); whole = encoding.GetString(buffer, 0, len); string[] lines = whole.Replace(" ", "\n").Split('\n'); int lineCount = lines.Length; if (halfLine != null) lines[0] = halfLine + lines[0]; if (buffer[len - 1] != 10) { halfLine = lines[lines.Length - 1]; lineCount--; } else { halfLine = null; } for (int i = 0; i < lineCount; i++) { //YOUR CODE GOES HERE. NO NEED TO CHANGE ANYTHING ELSE!!! ProcessLine(lines[i]); } } } } catch (Exception ex) { //ERROR!! } } private static int globalLine = 0; private static void ProcessLine(string line) { Console.WriteLine(globalLine + ". " + line); globalLine++; } } }
You need to login to post a comment.
