-9

Hi i have read this question :

Reading very large text files, should I be incorporating async?

I diged the net especially the STACK OVERFLOW !

The results was 14 method to do this but none of them is not complete !

In 2 last days , i am working on this and tested and benchmarked 14 methods.

for example :

        private void method()
        {

        FileStream FS = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

        int FSBytes = (int) FS.Length;

        int ChunkSize = 24;

        byte[] B = new byte[ChunkSize];

        int Pos;

        for (Pos = 0; Pos < (FSBytes - ChunkSize); Pos += ChunkSize)

        {

        FS.Read(B,0 , ChunkSize);
        string content = System.Text.Encoding.Default.GetString(B);

        richTextBox1.Text=content=;


        }

        B = new byte[FSBytes - Pos];

        FS.Read(B,0, FSBytes - Pos);
        string content2 = System.Text.Encoding.Default.GetString(B);

        richTextBox1Text=content2;


        FS.Close(); 
        FS.Dispose();
        }

for 5mb text file , it takes too long , what should i do ?

Community
  • 1
  • 1
Smart Man
  • 225
  • 1
  • 3
  • 16
  • 3
    `please help me with example code to do this` First *you* show, what you have done so far. – I4V Aug 07 '13 at 20:50
  • 4
    You want a user to scroll through 1Gb of text? I think you need to rethink your approach. –  Aug 07 '13 at 20:51
  • This is excessively impractical. I would split the file into multiple chunks using C first, and then allow the user to navigate between those chunks using buttons. – Forest Kunecke Aug 07 '13 at 20:53
  • 1GB of text would be 153,391,689 words. Assuming an average of 7 letters per word. What's your actual file like? – Logarr Aug 07 '13 at 20:54
  • @Logarr a log file for ex. – I4V Aug 07 '13 at 20:57
  • @FKunecke Notepad++ do read the whole file to some extent. although I don't recommend it for windows forms the idea is not that bad – Alaa Jabre Aug 07 '13 at 20:57
  • 1
    @MikeW What is wrong with that approach. In old times, people used similar tricks to edit large files using a limited memory such as 4K or 64K. – I4V Aug 07 '13 at 21:02
  • @FKunecke what would be the benefit of `C` when dealing with files? – I4V Aug 07 '13 at 21:04
  • @I4V 1Gb of text is roughly the same amount of text as 4000 average novels.It's not reasonable to expect someone to navigate their way through that in a single block, scrolled or otherwise. –  Aug 07 '13 at 21:07
  • @I4V I would use C for its memory efficiency. When dealing with ~1gb files I think a well thought-out C program could be more efficient than one written in C#. – Forest Kunecke Aug 07 '13 at 21:07
  • 1
    @FKunecke I am assuming you can prove that `C` is more *efficient*, instead of repeating some beliefs.(especially when File I/O is in question) – I4V Aug 07 '13 at 21:10
  • @MikeW I have more larger log files (text). And I would like to scroll, search text on it(as in notepad). What is wrong with it. – I4V Aug 07 '13 at 21:12
  • check this [question](http://stackoverflow.com/questions/18116400/begininvoke-cause-application-hang-in-backgroundworker/) out, I was working on an answer to your question and my answer but I ended up having troubles too! I hope this helps – Alaa Jabre Aug 08 '13 at 02:20
  • @SmartMan glad I was able to help. – Alaa Jabre Aug 08 '13 at 10:13

1 Answers1

1

This is a working example of reading a text file per stream to accomplish what you are trying to do. I have tested it with a 100 MB text file, and it worked well, but you have to see if larger files work as well.

This is the example. Just bring a RichTextBox to your form and a VScrollBar. Then use a file 'test.txt' on your hard drive 'C:'.

public partial class Form1 : Form
{
    const int PAGE_SIZE = 64;   // in characters
    int position = 0;  // position in stream

    public Form1()
    {
        InitializeComponent();
    }

    private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        position = e.NewValue * PAGE_SIZE;

        ReadFile(position);    
    }

    private void ReadFile(int position)
    {
        using (StreamReader sr = new StreamReader(@"C:\test.txt"))
        {
            char[] chars = new char[PAGE_SIZE];
            sr.BaseStream.Seek(position, SeekOrigin.Begin);
            sr.Read(chars, 0, PAGE_SIZE);

            string text = new string(chars);
            richTextBox1.Text = text;
        }    
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ReadFile(position);
    }
}
Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45
  • wouldn't richTextBox1.Text = text; replace old value with the new? – Alaa Jabre Aug 08 '13 at 11:48
  • Yes. When you scroll, the content of the RichTextBox is replaced with the new 'page'. – Kai Hartmann Aug 08 '13 at 11:52
  • Get the number of characters in the file like this: http://stackoverflow.com/questions/8214772/counting-number-of-characters-in-a-file-c-sharp Then divide that number by PAGE_SIZE. You probably will have to round up the result with Math.Ceiling(). So it would be something like: vScrollBar1.Maximum = Math.Ceiling(NumberOfCharacters / PAGE_SIZE); – Kai Hartmann Aug 09 '13 at 06:24
  • Carriage Return, LineFeed, and the combination of both are the identifiers for ReadLine() to identify the end of a line. http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx So you would have to extend your code, so that it reads characters until these characters. Afterwards you would have to update 'position' accordingly. – Kai Hartmann Aug 12 '13 at 06:24
  • @ Kai Hartmann : +1 For you. thanks. – Smart Man Aug 12 '13 at 14:51