Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Following snippet reads CSV Line count using BinaryReader.

Currently it checks \r and \n for line delimiters.

        private static int GetLineCount(string fileName)
        {
            BinaryReader reader = new BinaryReader(File.OpenRead(fileName));
            int lineCount = 0;

            char lastChar = reader.ReadChar();
            char newChar = new char();

            do
            {
                newChar = reader.ReadChar();
                if (lastChar == '\r' && newChar == '\n')
                {
                    lineCount++;
                }
                lastChar = newChar;
            } while (reader.PeekChar() != -1);
            return lineCount;
        }

I want to use Environment.NewLine string and make it work on windows\unix.

I want to refactor above to find word occurance and then match for the word Environment.NewLine. The issue is that I am not able to refactor following for word (more specifically change lastChar , newChar into Array.

                do
                {
                    newChar = reader.ReadChar();
                    if (lastChar == '\r' && newChar == '\n')
                    {
                        lineCount++;
                    }
                    lastChar = newChar;
                } while (reader.PeekChar() != -1);
share|improve this question
1  
Both BinaryReader and File.OpenRead() are IDisposable resources - wrap them in a using statement for deterministic disposal. –  Jesse C. Slicer Jan 12 '13 at 17:10
    
Wait, are you trying to do line count or an array comparison (as the title suggests)? Also, do you need an exact count in the case of a large file? If you know the total file size and you know the length of the first 20,000 lines, then you could have a good guess of what the line count is approximately. What are you trying to do at the higher level? –  Leonid Jan 12 '13 at 18:55
    
Array-comparison (or specifically word comparison) comes into picture as I want to use Environment.NewLine. At higher level I am finding exact line count for big files ( >1GB) for progress reporting. Currently It is done using Stream Current Read position divided by file length. But that is not acceptable (as inconsistent due to other file types involved), in our overall context. –  Tilak Jan 12 '13 at 19:00
add comment

1 Answer 1

up vote 3 down vote accepted

Environment.NewLine always returns \r\n so it won't help you in parsing different line endings.

If your task is to count the number of lines then it would be much easier just to do smth. like:

    private static int GetLineCount(string fileName)
    {
        return File.ReadLines(fileName).Count();
    }

ReadLines method automatically parses different line endings.

share|improve this answer
2  
1. Environment.NewLine returns line feed. On unix it is LF (\n), on Mac it is CR(\r), on windows it is CRLF(\r\n). 2. Try running the code on large files (> 2 GB), It will throw OutOfMemoryException. –  Tilak Jan 12 '13 at 12:57
    
@Tilak: Why would you line count a 2gb file?... –  acidzombie24 Jan 12 '13 at 19:49
    
2GB is for illustration, Files are large in size (100MB - 1GB), but to answer your question, I need to show message like Processing x row in Total N rows. –  Tilak Jan 12 '13 at 19:53
1  
@Tilak, I've used ReadLines method that returns a stream of lines. You probably confused it with ReadAllLines which indeed loads all lines in memory. The only case when this code would throw OutOfMemoryException is when your file would have a line longer than 2GBs which is very-very unlikely for text files –  almaz Jan 12 '13 at 20:22
    
@Tilak about Environment.NewLine - are you talking about running this .NET code on Mac/Unix? Or parsing files coming from those platforms? .NET framework explicitly specifies \r\n as a value for Environment.NewLine, not sure about Mono. –  almaz Jan 12 '13 at 20:30
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.