Click here to Skip to main content
11,577,429 members (39,052 online)
Click here to Skip to main content

Fast CSV Row Count using Binary Reader

, 5 Feb 2013 CPOL 15.1K 5
Rate this:
Please Sign up or sign in to vote.
Efficient CSV row/line count snippet using BinaryReader API on Windows.


The code snippet in this article illustrates an efficient/fast row/line count algorithm using the BinaryReader API.


CSV file format has text data. There is no official restriction on the number of rows, number of columns, or file size. 

Due to no restrictions, to read the number of lines, a complete file reading is required. 

Using the Code 

In the Windows operating system a line break is represented by CRLF (\r \n). The basic approach is to read all the content through streaming, and find the line breaks. The BinaryReader API is used for stream reads. This class reads primitive data types as binary values in a specific encoding. 

private static int GetLineCount(string fileName)
    BinaryReader reader = new BinaryReader(File.OpenRead(fileName));
    int lineCount = 0;
    char lastChar = reader.ReadChar();
    char newChar = new char();
        newChar = reader.ReadChar();
        if (lastChar == '\r' && newChar == '\n')
        lastChar = newChar;
    } while (reader.PeekChar() != -1);

    return lineCount;

The above snippet checks for CRLF that is line break on Windows. The code can be further improved by checking the Environment.NewLine property that specifies new line string for the environment.

  • For Unix environment, new line string is LF (\n).  
  • For Mac environment, new line string is CR (\r).   


  1. Read all records at a time, and calculate the array length using the File.ReadAllLines API. This is good for small files. For large files (>2GB) an OutOfMemoryException is expected. 
  2. StreamReader API: There are two options:
    1. using the ReadLine function to read lines. This has a trade-off of line to string conversion which is not needed.
    2. using the Read() and Peek() methods. This is similar to using the BinaryReader approach but these methods return integer and not char so a little bit more logic is required for character comparisons. 

Points of Interest  

Below are some efficient CSV parsers that I have come across/used.

  1. TextFieldParser: This is built-in .NET structured text file parser. This parser is placed in the Microsoft.VisualBasic.dll library. 
  2. KBCsv library: This is an efficient, easy to use library developed by Kent Boogaart


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Software Engineer based out in Noida.

Technology skillset – .NET, WPF, WCF, LINQ, XAML.

Started blogging on

Stackoverflow Profile ->

You may also be interested in...

Comments and Discussions

BugStreamReader is more efficient Pin
srinivasraos17-Sep-14 23:57
membersrinivasraos17-Sep-14 23:57 
SuggestionSuggestion... Pin
Andrew Rissing14-Jan-13 4:47
memberAndrew Rissing14-Jan-13 4:47 
You might want to take advantage of using statements. Furthermore, if you look in a reflection type tool at the calls you're making, BinaryReader.ReadChar is just calling BinaryReader.Read. Read is preferrable over ReadChar as it allows you to check for end of stream (-1), rather than using peek.

In addition, you can tweak the code to be flexible to account for either encoding, doing the following:

private static int GetLineCount(string fileName)
    int lineCount = 0;
    int last = -1;
    int current;
    using (FileStream fs = File.OpenRead(fileName))
    using (BinaryReader reader = new BinaryReader(fs))
        while ((current = reader.Read()) != -1)
            if ((current == '\n') || (current == '\r'))
                // Advance past the next character as it is part of the prior line.
                if ((current != last) && (last != -1))
                last = current;
        return lineCount;

I haven't executed/tested this code, but that should be the right answer.

Also, if you're going to go through the trouble of parsing for line count, you might as well do something with that data. I can't think of many use cases where you'd care to know this information and not want to eventually parse out the data within that file. But, either way, you have your function now.
GeneralRe: Suggestion... Pin
Fun@learn14-Jan-13 7:27
memberFun@learn14-Jan-13 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 5 Feb 2013
Article Copyright 2013 by Fun@learn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid