1
\$\begingroup\$

I have following code in QT in

// myEditor.h
class myEditor : QScintilla {
    public:
        readFile();
};

#include "myEditor.h"
// myEditor.cc
myEditor::readFile() {
   FILE* fp = fopen("mynew.v","r"):
   QTextStream ts(fp, QIODevice::ReadOnly);
   /* reading the  text stream  buffer by buffer
   bufferSize is calculated using following formula
   2 to power(k) *  n  =  2 to power 31*
   where n is  size of each block in linux filesystem*/

   int bufferSize =(1024* 1024)/2;
   do {
      QString s = ts.read(bufferSize);
      append(s);
    } while(!ts.atEnd());
}

I calculated the bufferSize as per comments .It will be helpful if someone can review the code and let me know if there are issues.

\$\endgroup\$
3
  • \$\begingroup\$ for comment 1 , legacy code was like that but it crashed when size of file is greater than 500 MB and we have such use cases . \$\endgroup\$ Commented Jun 9, 2016 at 10:31
  • \$\begingroup\$ For comment3 , I am not sure whether QFile will open .gz file \$\endgroup\$ Commented Jun 9, 2016 at 10:32
  • \$\begingroup\$ Hi, because the comments are for the answer, they should be placed there, not underneath the question. Are you trying to display 500MB of text in QScintilla? I'm sure it wasn't designed for this. As for gzip files, QFile, by itself, doesn't handle (de)compression, but neither does fopen() \$\endgroup\$ Commented Jun 9, 2016 at 17:40

1 Answer 1

2
\$\begingroup\$

As you're storing the whole result into memory anyway for display in a text editor (so you're not expecting to load hundreds of MB of text), why not just use what Qt provides to read the entire file into a string in one call?

It's probably more than enough optimized for general purpose usage. Displaying the entire result in the editor might also be faster than constantly appending parts.

I also see that you're using the C API for opening the file (which is never closed) and then passing it to Qt. Qt has QFile for this, as shown in the link mentioned above.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.