Search code examples
c#.nettext-filesstreamreaderflat-file

C# Streamreader - Break on {CR}{LF} only


I am trying to count the number of rows in a text file (to compare to a control file) before performing a complex SSIS insert package.

Currently I am using a StreamReader and it is breaking a line with a {LF} embedded into a new line, whereas SSIS is using {CR}{LF} (correctly), so the counts are not tallying up.

Does anyone know an alternate method of doing this where I can count the number of lines in the file based on {CR}{LF} Line breaks only?

Thanks in advance


Solution

  • Iterate through the file and count number of CRLFs.

    Pretty straightforward implementation:

    public int CountLines(Stream stream, Encoding encoding)
    {
        int cur, prev = -1, lines = 0;
        using (var sr = new StreamReader(stream, encoding, false, 4096, true))
        {
            while ((cur = sr.Read()) != -1)
            {
                if (prev == '\r' && cur == '\n')
                    lines++;
    
                prev = cur;
            }
        }
    
        //Empty stream will result in 0 lines, any content would result in at least one line
        if (prev != -1)
            lines++;
    
        return lines;
    }
    

    Example usage:

    using(var s = File.OpenRead(@"<your_file_path>"))
        Console.WriteLine("Found {0} lines", CountLines(s, Encoding.Default));
    

    Actually it's a find substring in string task. More generic algorithms can be used.