Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 93657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:22:44+00:00 2026-05-10T23:22:44+00:00

Please feel free to correct me if I am wrong at any point… I

  • 0

Please feel free to correct me if I am wrong at any point…

I am trying to read a CSV (comma separated values) file using .NET file I/O classes. Now the problem is, this CSV file may contain some fields with soft carriage returns (i.e. solitary \r or \n markers rather than the standard \r\n used in text files to end a line) within some fields and the standard text mode I/O class StreamReader does not respect the standard convention and treats the soft carriage returns as hard carriage returns thus compromising the integrity of the CSV file.

Now using the BinaryReader class seems to be the only option left but the BinaryReader does not have a ReadLine() function hence the need to implement a ReadLine() on my own.

My current approach reads one character from the stream at a time and fills a StringBuilder until a \r\n is obtained (ignoring all other characters including solitary \r or \n) and then returns a string representation of the StringBuilder (using ToString()).

But I wonder: is this is the most efficient way of implementing the ReadLine() function? Please enlighten me.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:22:44+00:00Added an answer on May 10, 2026 at 11:22 pm

    It probably is. In terms of order, it goes through each char once only, so it would be O(n) (where n is the length of the stream) so that’s not a problem. To read a single character a BinaryReader is your best bet.

    What I would do is make a class

    public class LineReader : IDisposable {     private Stream stream;     private BinaryReader reader;      public LineReader(Stream stream) { reader = new BinaryReader(stream); }      public string ReadLine()     {         StringBuilder result = new StringBuilder();         char lastChar = reader.ReadChar();         // an EndOfStreamException here would propogate to the caller          try         {             char newChar = reader.ReadChar();             if (lastChar == '\r' && newChar == '\n')                 return result.ToString();              result.Append(lastChar);             lastChar = newChar;         }         catch (EndOfStreamException)         {             result.Append(lastChar);             return result.ToString();         }     }      public void Dispose()     {         reader.Close();     } } 

    Or something like that.

    (WARNING: the code has not been tested and is provided AS IS without warranty of any kind, expressed or implied. Should this program prove defective or destroy the planet, you assume the cost of all necessary servicing, repair or correction.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please feel free to correct me if I am wrong. In JVM heap, there
I'm probably going about this the wrong way, please feel free to correct me.
Please feel free to correct any miss used words in this post, as I'm
Please feel free to move this to meta/superuser if this is the wrong place.
Please feel free to add multiple answers, each with a single point, to make
Strange issue that I'm not exactly sure about...also, please feel free to correct me
Please feel free to correct me, but from what I understand of the bootstrap
First of all, please feel free to correct the title of my question, i'm
I'm not sure this is the right forum if it's not please feel free
Feel free to correct my terminology, and understanding of the List if needed.. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.