0

so I'm trying to load a file into a richtextbox without using the LoadFile function, as it does not allow you to specify an Encoding. I also want it to load in chunks, so it does not use as much memory. I have tried MANY ways, using a binary reader, streamreader, etc, but I think this is the best solution. Unfortunately, no matter what I do, whenever I compare the original file and the loaded file in Notepad++, there always seems to be extra lines in the text file. I thought it was a problem with reading the file, and I did a test to find it out. I have the input file stream and output file, and they were exactly the same! However, when I append the text in the richtextbox, a few extra lines appear.

Here is the code:

    const int bufferSize = 16384;
    using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan))
                {
                    using (FileStream outStream = File.Create(@"C:\Users\me\Desktop\file comparison.txt"))
                    {
                        int bytesRead;
                        byte[] buffer = new byte[bufferSize];
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            outStream.Write(buffer, 0, bytesRead);
                    }
                }
    //Result:
    //The 2 files are the same

    using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan))
                {
                    int bytesRead;
                    byte[] buffer = new byte[bufferSize];
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        if (bytesRead != bufferSize)
                            Array.Resize(ref buffer, bytesRead); //On the last chunk if the file is not a multiple of bufferSize (16384), it will leave some parts of the previous chunk behind
                        richTextBox.AppendText(Encoding.UTF8.GetString(buffer));
                    }
                }
//Result:
//When I copy and paste into notepad++ and compare the original and this, there are a few extra lines (empty lines)

Does anybody know what's wrong? Thanks.

Visual User
  • 69
  • 1
  • 6
  • You cannot use `Encoding.X.GetString()` in that manner. Maybe use [File.ReadLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines) instead. It's also useless, since you have to load that file in memory anyway. Btw, [RichTextBox.LoadFile()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.loadfile) has an overload that accepts a Stream – Jimi Dec 29 '19 at 21:40
  • But i thought i was loading the file in chunks into the memory? – Visual User Dec 29 '19 at 21:46
  • An encoded text has specific byte boundaries. You cannot read a generic chunk of bytes and hope that you get back a fully formed string from it. You have to reach a line boundary (marked with `\n` (`0x10`) or `\r\n` (`0x13;0x10`)). But it's pointless if you end up loading the entire file in memory anyway. If you want to implement a sort of *pagination*, you have to read a number of lines at a time, possibly buffering them. See the example [here](https://stackoverflow.com/a/59222329/7444103), it's something similar. – Jimi Dec 29 '19 at 21:53
  • Ok, thank you very much. I will try to read more lines at a time, like 50 lines at a time. – Visual User Dec 29 '19 at 22:01
  • Hi, I was just wondering, how do I specify the encoding with richtextbox.LoadFile? – Visual User Dec 29 '19 at 23:17
  • Since you're passing a Stream, you can create a [StreamReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.-ctor) yourself, use the stream returned [OpenFileDialog.OpenFile()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog.openfile) etc. – Jimi Dec 30 '19 at 05:55

0 Answers0