jhonny625 jhonny625 - 18 days ago 7
C# Question

its possible make a stream reader that Read 5 lines at a time instead of one?

I use a

StreamReader
but it only reads one line at a time.

My question is: Is it possible to change this behaviour so I can read more than one? For example: 5 lines at a time?

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
class example
{
public static void Main()
{
var path = "pruebastream.txt";
var path2 = "pruebastream2.txt";
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
using (StreamWriter outputFile = new StreamWriter(path2))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var text = ProcessLine(line);
outputFile.WriteLine(text);
}
}
}
static string ProcessLine(string text)
{
}
}

Answer

This might get downvoted a bit, but easier approach can be to just use 5 .ReadLine:

using (var sr = File.OpenText("1.txt"))
using (var sw = File.CreateText("2.txt"))
{
    while (!sr.EndOfStream)
    {
        var s = sr.ReadLine() + "\r\n" + sr.ReadLine() + "\r\n" + 
            sr.ReadLine() + "\r\n" + sr.ReadLine() + "\r\n" + sr.ReadLine();
    }
}

Otherwise, use counter variable if you don't want the extra newline characters after the last few lines.