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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:33:50+00:00 2026-05-15T00:33:50+00:00

I’d like to make a simple text file viewer and I’d like it to

  • 0

I’d like to make a simple text file viewer and I’d like it to be able to handle large files (possibly larger than the computer’s memory).

I know that I need to implement something like a sliding buffer, that will contain the currently visible portion of the file. The main problem is to determine the relation between lines and file offsets. If I just needed to be able to navigate by lines, I’d just need an linked list of lines and on line up/line down just read new line from the file. But what should I do when I also want to go to, say 50% of the file? I need to show the lines starting from the half of the file, so if the file is 10000 bytes long, I’d seek to byte 5000, look for a line break and display stuff from there. The problem is, that I don’t know what line I’m at when seeking like this.

So what I would like to know is what would be a suitable data structure for keeping these few lines in memory (the ones that will be painted on the screen).

Keep in mind that I don’t need to edit the files, just view them, so I don’t need to care about efficiency of the chosen approach for editing.

  • 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-15T00:33:51+00:00Added an answer on May 15, 2026 at 12:33 am

    If you’re reading in a defined chunk of bytes via a FileStream you could keep track of which byte you read last so you know where to pick up next to read more data chunks from the file. FileStream exposes Read() which allows you to specify an offset byte (position to start) and also how many bytes to read at a time.

    After you read in your bytes you can decode them to UTF8 with a decoder, for instance, and then retrieve a char array with it. All of that should initialize your initial data. What I would do since this will be displayed somewhere is setup event handlers tied to scrolling. When you start scrolling down you can remove top lines from memory (at the same time counting their bytes before deleting so you can dynamically read in the next set bytes with the same exact size) and append new lines to the bottom. Likewise for scrolling upward.

    If you’re wanting to figure out half of your data then you could try something with makign a FileInfo object on the text file path and then using the Length() method to return the number of bytes. Since streams deal in bytes this comes in handy when trying to read in a percentage. You can use that to define how many bytes to read in. You’ll have to read data in to determine where line breaks are and set your last byte read as the CR-LF to pickup at the next line when you retrieve data again.

    Here’s what I would do to read a predefined count of bytes from a file.

    public static LastByteRead = 0; // keep it zero indexed
    
    public String[] GetFileChunk( String path, long chunkByteSize )
    {
        FileStream fStream;
        String[] FileTextLines;
        int SuccessBytes = 0;
        long StreamSize;
        byte[] FileBytes;
        char[] FileTextChars;
        Decoder UtfDecoder = Encoding.UTF8.GetDecoder();
        FileInfo TextFileInfo = new FileInfo(path);
    
        if( File.Exists(path) )
        {
            try {
                StreamSize = (TextFileInfo.Length >= chunkByteSize) ? chunkByteSize : TextFileInfo.Length;
                fStream = new FileStream( path, FileMode.Open, FileAccess.Read );
                FileBytes = new byte[ StreamSize ];
                FileTextChars = new char[ StreamSize ]; // this can be same size since it's UTF-8 (8bit chars)
    
                SuccessBytes = fStream.Read( FileBytes, 0, (Int32)StreamSize );
    
                if( SuccessBytes > 0 )
                {
                    UtfDecoder.GetChars( FileBytes, 0, StreamSize, FileTextChars, 0 );
                    LastByteRead = SuccessBytes - 1;
    
                    return 
                        String.Concat( fileTextChars.ToArray<char>() ).Split('\n');
                }
    
                else
                    return new String[1] {""};
            }
    
            catch {
                var errorException = "ERROR: " + ex.Message;
                Console.Writeline( errorException );
            }
    
            finally {
                fStream.Close();
            }   
        }   
    }
    

    Maybe that will get you in the right direction at least.

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

Sidebar

Related Questions

No related questions found

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.