Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Early Question
September 20, 2010 02:17PM
Hi all. I have only really started studying this module now for work reasons, so would appreciate a bit of help with some early questions;

Assuming I have a text file "Bren.txt", with one line of text in it, namely "Brendan", why would the following code overwrite this files contents rather than append the new line I ask it to in the program segment to the existing contents of the file?

//main.cpp

#include <QApplication>
#include <QString>
#include <QTextStream>
#include <QFile>

int main(int argc, char* argv[])
{
QApplication myapp(argc, argv);
QFile data("Bren.txt" ) ;

if(data.open(QFile::ReadWrite|QFile::Append))
{
QTextStream out(&data);
out << " Add this line";

}
return myapp.exec();
}

The output to the file always comes up as

" Add this line"

rather than

"Brendan Add this line"

How do I rectify this??

THX

vv
avatar Re: Early Question
September 20, 2010 02:35PM
Try leaving off the QIODevice::ReadWrite. Use QIODevice::Append on its own.

(just a thought, I've not tried this myself.)
Re: Early Question
September 20, 2010 02:52PM
Hi robanaurochs,

Yeah I already tried that technique earlier and it seemed to produce the same result, hence I though I would ask for some help.

Tried it again anyways, and still seems to not want to append for some reason.

Not entirely sure why not, because the documentation seems to indicate it would.

sad smiley
avatar Re: Early Question
September 20, 2010 05:10PM
Or try this:
Language: C++
(QIODevice::WriteOnly |QIODevice::Append | QIODevice::Text);

I've tested it on my system and it works smiling smiley
Re: Early Question
September 20, 2010 08:00PM
Thx for that info CountZero. just to clarify however, when I have a source text file, I need to place it in the same directory as my main source file. and after compilation and subsequent run, the resultant text file displays in the debug folder am I correct?

Having done it as above, still cannot seem to get the correct output. Wonder what I could be doing wrong?? Could you explain how you got your output correct?

Cheers

VV
Anonymous User
Re: Early Question
September 21, 2010 07:24AM
I run exactly (I copied and pasted) the same code as you provided in your first post and get the desired result (i.e. Brendan Add this line). I'm not sure which version of Qt you're using, but I'm running 4.7 and 4.7 automatically creates two directories for your project, one includes all your source and project files and the other your makefiles, a "debug" folder and a "release" folder (i.e. your entire build). I stuck the txt file into the build directory (i.e. not the one with my main file) and everything worked brilliantly...If you have only one directory which includes all your files (source as well as build), then the txt file should simply go in that same directory. Another option is to add a resource file to your project (your txt file...obviously)...If none of this works, a simple way to test where Qt wants your txt file is to run your code with no existing txt file and see where Qt puts it (i.e. find "Bran.txt" after running the code in your original post).

Edit: You must also pay attention to whether you build your project in "debug" or "release" mode as this may affect where the txt file is created if it doesn't yet exist.
avatar Re: Early Question
September 21, 2010 09:47AM
I doubt it's the version. I've been using Qt since 4.1.1 and I'm now on 4.6.2 (waiting for the official release before upgrading to 4.7). In all versions, even with the VS add-ins, those two directories are created.
avatar Re: Early Question
September 21, 2010 10:41AM
Why don't you just code the full path to your file, eg
Language: C++
QFile data("c:/projectname/Bren.txt" ) ;

Then you know that you are working with the same file.
avatar Re: Early Question
September 21, 2010 12:07PM
It would be better to let Qt give you the correct path.

Language: C++ (QT)
// fileName contains the name of the file, either entered from the console or hard-coded into the program QString filePath = QString("%1/%2").arg(QCoreApplication::applicationDirPath(), fileName); QFile data(filePath);
Re: Early Question
September 21, 2010 01:57PM
Thx guys, I will review all the info you guys have posted and let you know. Much appreciated
Re: Early Question
September 21, 2010 02:31PM
Thanks guys. I am sure all your recommendations work, but I seem to have success with Count Zero's recommendation to specify the full directory. This did indeed update the source file correctly, and it did update the original file as opposed to creating a new one in the debug directory.

I will try work on the other recommendations, but that one did indeed work.

Will post more ? in future

Thank u

vv
Sorry, only registered users may post in this forum.

Click here to login