0

How can read text from a file line by line?

This code who I used is read first and second line in first rotation. The following isn't working as it's returning two different strings in method sr.ReadLine(). Does the ReadLine() method take the next line from file rather than the current line?

List<string> allInformation = new List<string>();
DateTime minimumDateTime = times[this.Step].AddMinutes(-different);
DateTime maximumDateTime = times[this.Step].AddMinutes(different);

using (FileStream fs = System.IO.File.Open(this.File, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
    DateTime thisTime;
    string[] info = new string[6];

    while (sr.Peek() >= 0)
    {
        info = sr.ReadLine().Split(new string[] { ", ", ",", "\"" }, StringSplitOptions.RemoveEmptyEntries);
        thisTime = DateTime.ParseExact(info[FileConstants.DATE], "yyyy-M-d H:m:s", null);

        if (thisTime > minimumDateTime && thisTime < maximumDateTime)
        {
            allInformation.Add(sr.ReadLine());
        }
    }
}
Evgeni Velikov
  • 362
  • 3
  • 10
  • 28
  • 2
    You're calling `ReadLine` twice in your loop. Call it once and assign it to a local variable for latter use. – juharr Nov 12 '15 at 13:13
  • Like juharr said. twice the readline. Here is an alternative to the answer of Tim. http://stackoverflow.com/a/9643111/169714 – JP Hellemons Nov 12 '15 at 13:15
  • I fixed and is working correct. Thanks in everybody for fast answers. I can believe it how fast is answering me. – Evgeni Velikov Nov 12 '15 at 13:17

2 Answers2

4

You are using ReadLine two times in the loop. Store the return value of StreamReader.ReadLine in a string variable. Otherwise you are advancing the reader to the next line.

while (sr.Peek() >= 0)
{
    string line = sr.ReadLine();
    info = line.Split(new string[] { ", ", ",", "\"" }, StringSplitOptions.RemoveEmptyEntries);
    thisTime = DateTime.ParseExact(info[FileConstants.DATE], "yyyy-M-d H:m:s", null);

    if (thisTime > minimumDateTime && thisTime < maximumDateTime)
    {
        allInformation.Add(line);
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

I know you already have the answer, but I just wanted to point out that you can write that entire method more succinctly using Linq like this:

var minimumDateTime = times[this.Step].AddMinutes(-different);
var maximumDateTime = times[this.Step].AddMinutes(different);

var linesInDateRange = 
    from   line in System.IO.File.ReadLines(this.File)
    let    info = line.Split(new[] {", ", ",", "\""}, StringSplitOptions.RemoveEmptyEntries)
    let    thisTime = DateTime.ParseExact(info[FileConstants.DATE], "yyyy-M-d H:m:s", null)
    where  thisTime > minimumDateTime && thisTime < maximumDateTime
    select line;

var allInformation = linesInDateRange.ToList();
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Who is the fastest way. For me is very important to be so faster. I'm not good with linq and this code for me is not really readable. But if is faster then I used this code. When I tested is look the same speed with StreamReader code. – Evgeni Velikov Nov 12 '15 at 13:55
  • @EvgeniVelikovevelikov There will be effectively no difference in speed. The code for both approaches will be IO-bound; that is, the amount of time spent reading the file will far exceed the processing time, so you can't really make it go any faster. – Matthew Watson Nov 12 '15 at 14:03