2

I'm mostly using Qt framework in C++. Can anyone explain me the advantages of using textstream objects instead of using directly objects?

Here is an example code without a QTextStream;

QFile file("asd.txt");
// assuming that file exists
file.open(QIODevice::Append);
file.write("asd");
file.close();

What are the advantages (or disadvantages) of using below code instead the above one;

QFile file("asd.txt");
// assuming that file exists
file.open(QIODevice::Append);
QTextStream tStream(file);
file << "asd";
file.close();

Thanks in advance.

2 Answers2

6

QFile::write either writes a nul terminated C string, or binary data that you give it.

QTextStream on the other hand does text formatting/conversion.

  • It deals with text output/input only, not arbitrary binary data.
  • You can give it a primitive type (int, float, long etc.) and it'll convert it to a textual representation
  • You can have it read text and convert to primitive types.
  • You can have it do formatted output, e.g. pad or left/right adjust text.
  • You can set the text encoding (e.g. UTF-8, UTF-16)
  • It buffers the data, potentially resulting in fewer system calls being made. Note that this means your code should call tStream.flush(); before you close the file.
owacoder
  • 4,815
  • 20
  • 47
nos
  • 223,662
  • 58
  • 417
  • 506
  • *"should call `tStream.flush();` before you close the file"* - that would be a sign of very poor design. The docs explicitly say that [in the destructor](http://doc.qt.io/qt-4.8/qtextstream.html#dtor.QTextStream) *"flush() will be called implicitly"* if needed (and I'm sure it's a given it closes the stream too); similarly, I'd say it's a given that `close` would `flush`. Do you have any experience/reference to the contrary? FWIW, someone makes a similar assertion as you [here](http://stackoverflow.com/a/19096932/410767), saying `QSaveFile` must be used. – Tony Delroy Dec 10 '15 at 12:18
  • @TonyD The code of the OP first closes the file, then the QTextStream destructor runs and flushes its buffer. You wouldn't want to do that, surely you want to flush() the QTextStream before closing the file, (You could do that in better ways e.g. create a new scope just for the QTextStream and drop the explicit flushing, I'm just pointing out something the OP should be aware of) – nos Dec 10 '15 at 12:28
  • @nos: oh cool - I hadn't read over the OP's code that closely. Sorry for the noise. Cheers. – Tony Delroy Dec 10 '15 at 13:26
0

To complement the existing answer, note that streams are type-safe. In particular, you do not have the problems of scanf and pointers.

But even C++ guru Herb Sutter prefers snprintf-style syntax for some uses - I agree. It is sometimes easier to remember printf-style formatting syntax than the iostream formatting flags.

Also, beware that using actual printf and cout in the same program can incur severe performance penalties due to syncing of the internal objects used by libc printf and stdlibc++ cout. At least, that used to be the case.

Erik Alapää
  • 2,585
  • 1
  • 14
  • 25