PDA

View Full Version : Question about using QFile and QTextStream



liuliuwang
13th October 2010, 09:11
Hi, I have a question regarding QFile and QTextStream. I know that normally we will use them like this:



QFile file("filename.txt"); // create a QFile object by giving a file name(and path)
if (!file.open(QIODevice::Read/WriteOnly)) {
// print error
return;
}

QTextStream outStream(&file); // create a QTextStream object which "link" to the QFile object "file"


my question is: can we use QTextStream alone? That is, instead of create a QFile first and then create a QTestStream pointing to that QFile, can we create a QTextStream directly from a file name and then read or write directly on this QTextStream object?

I used to use StreamWriter and StreamReader in C# and hope to have something similar in QT classes. So far, I have to always keep two objects, one QFile and one QTextStream, in order to read/write a file.

Thanks for any comment!

tbscope
13th October 2010, 09:25
No, if you want to write or read from a file, you need to have a file descriptor.
Either pass a FILE object to the textstream or a QIODevice object.

You can create a subclass of course that encapsulates everything.

liuliuwang
13th October 2010, 09:45
hi, tbscope, thank you for the quick reply !

The task I am facing is to keep a list (QList) of txt files open during runtime and write into these files frequently, and close all files when program ends.
Right now, I implemented in this way: each element in the QList is a struct, which consists of a QString (holding the full path of file), a QFile pointer, and a QTextStream pointer, and some other data members.
Just wondering if there is a way to only use one pointer.
actually, the reason I need to keep a QTextStream object is to use the ReadLine() function, since QFile doesn't have this function.
Also I am wondering why QTextStream doesn't provide a Writeline() function...

thanks again !

ChrisW67
13th October 2010, 23:33
The task I am facing is to keep a list (QList) of txt files open during runtime and write into these files frequently, and close all files when program ends.
Right now, I implemented in this way: each element in the QList is a struct, which consists of a QString (holding the full path of file), a QFile pointer, and a QTextStream pointer, and some other data members.
Just wondering if there is a way to only use one pointer.

You have a QList<YourStruct> or QList<YourStruct *> so you are already providing a single "pointer" access to each set of data.

If you keep a handle on the QTextStream then you can always access the underlying QIODevice through QTextStream::device(). That will allow you to close the device (file). If you qobject_cast the QIODevice* to a QFile* then you can retrieve the QFile::fileName() also.

Also I am wondering why QTextStream doesn't provide a Writeline() function...
readLine() is a convenience method to read a line where it automatically handles different line ending conventions for you. The overloaded operator<<() and the related endl function provide the write functionality with the 'correct' line ending for your platform.