PDA

View Full Version : QTextStream and file handle



Dilshad
4th January 2011, 11:14
Hi,
I have a query regarding Files and textstream in QT

if I have a code such as the following :


QFile file("abc.txt");
file.open(QIODevice::ReadOnly);
QTextStream(&file);

file.setFileName("xyz.txt");
file.open(QIODevice::ReadOnly);

In the above case what should QTextstream be pointing at?
Contents of "abc.txt" or "xyz.txt"
In my case it points to "abc.txt"
Is there any way I can change it to point to "xyz.txt"?

Thank you in advance.

high_flyer
4th January 2011, 11:21
In my case it points to "abc.txt"
This surprises me, and I tend to think that either the code you posted is not 100% the same as you really use it, or, something else that you do.
The stream is not 'pointing' to anything, it takes a reference to a QFile (actually a QIODevice) object.
This means, the stream doesn't know anything about the file name - it just streams to the device it has a reference to, and that device may have any filename what so ever - the QFile object is reposible for opening the physical file, not the stream.

Make sure you are not setting a different file name to an already open QFile object.

franz
4th January 2011, 11:26
In this case I would advice you to go with the core python paradigm: Explicit is better than implicit. Remember that you will be having to read and maintain this code at some point, and take it from me, you'll have forgotten how this piece of code was supposed to work in three weeks time. Create a new file and a new text stream to avoid any confusion now and in the future.