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 715269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:10:05+00:00 2026-05-14T05:10:05+00:00

My simple requirement: Reading a huge (> a million) line test file (For this

  • 0

My simple requirement: Reading a huge (> a million) line test file (For this example assume it’s a CSV of some sorts) and keeping a reference to the beginning of that line for faster lookup in the future (read a line, starting at X).

I tried the naive and easy way first, using a StreamWriter and accessing the underlying BaseStream.Position. Unfortunately that doesn’t work as I intended:

Given a file containing the following

Foo
Bar
Baz
Bla
Fasel

and this very simple code

using (var sr = new StreamReader(@"C:\Temp\LineTest.txt")) {
  string line;
  long pos = sr.BaseStream.Position;
  while ((line = sr.ReadLine()) != null) {
    Console.Write("{0:d3} ", pos);
    Console.WriteLine(line);
    pos = sr.BaseStream.Position;
  }
}

the output is:

000 Foo
025 Bar
025 Baz
025 Bla
025 Fasel

I can imagine that the stream is trying to be helpful/efficient and probably reads in (big) chunks whenever new data is necessary. For me this is bad..

The question, finally: Any way to get the (byte, char) offset while reading a file line by line without using a basic Stream and messing with \r \n \r\n and string encoding etc. manually? Not a big deal, really, I just don’t like to build things that might exist already..

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-14T05:10:06+00:00Added an answer on May 14, 2026 at 5:10 am

    You could create a TextReader wrapper, which would track the current position in the base TextReader :

    public class TrackingTextReader : TextReader
    {
        private TextReader _baseReader;
        private int _position;
    
        public TrackingTextReader(TextReader baseReader)
        {
            _baseReader = baseReader;
        }
    
        public override int Read()
        {
            _position++;
            return _baseReader.Read();
        }
    
        public override int Peek()
        {
            return _baseReader.Peek();
        }
    
        public int Position
        {
            get { return _position; }
        }
    }
    

    You could then use it as follows :

    string text = @"Foo
    Bar
    Baz
    Bla
    Fasel";
    
    using (var reader = new StringReader(text))
    using (var trackingReader = new TrackingTextReader(reader))
    {
        string line;
        while ((line = trackingReader.ReadLine()) != null)
        {
            Console.WriteLine("{0:d3} {1}", trackingReader.Position, line);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple requirement: I want to add a set of strings to
the Requirement in simple words goes like this. Its a charting Application ( kinda
I have a simple requirement: I have millions of strings, and want to test
I'm reading in a large text file with 1.4 million lines that is 24
We have a simple requirement to use https for certain specific pages in a
I have a simple requirement, i need a map of type . however i
I have a simple requirement as mentioned below: A ListView or any control displays
I am having a very simple requirement.I need the border on left and right
I am using django 1.4 and Python 2.7. I just have a simple requirement
I have an ASP ListView, and have a very simple requirement to display numbers

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.