PDA

View Full Version : writing to a text-file



QtBros61
7th April 2010, 09:22
I have made a simple qt4-app with Carbide ide.
In one of my functions i am trying to write some text to a text-file but it is not working. I added file name result.txt to the project in Carbide.

Here is the code:


QFile file("result.txt");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << QString("victory"); // serialize a string


I also have included <QFile> and <QDataStream> in cpp-file.

The program dosent show any errors but it dosent write anythink to the file.

wysota
7th April 2010, 09:58
What does QFile::open() return?

QtBros61
7th April 2010, 11:08
I tried to use debbugger to check the return value but there was just a lot of variables and values like QIODevice- folder->QObject->d_ptr->d = 0x424cc19c
and
QIODevice- folder->QObject->d_ptr->d->unused=3651511 so from those i dont know..

i added this code in the function:



QFile file("result.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox msgBox;
msgBox.setText("File opened");
msgBox.exec();
}


And i did get the message. So the problem aint in opening the text-file?
I am running the program in windows xp.

JD2000
7th April 2010, 13:05
Have you tried
QFile file;
file.open("result.txt", QIODevice::WriteOnly);

QtBros61
7th April 2010, 14:00
i got the following error: function call '[QFile].open({lval} const char[9], QIODevice::OpenModeFlag)' does not match

i also chaged the messagebox code to this:



QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
}


In debugger values for example. line-d->data= Can't read memory from address 0xCCCCCCDC

wysota
7th April 2010, 16:06
And i did get the message. So the problem aint in opening the text-file?
But you checked it for reading, not for writing.

ChrisW67
8th April 2010, 09:15
As an aside, do you want to write a human readable text file or a binary file that contains some text? Use QTextStream and QDataStream respectively. Your first post used QDataStream and your last post used QTextSTream, which is a recipe for confusion.

kevinchannon
9th April 2010, 11:15
ChrisW67 is correct. To write text, you should use QTextSream. Although, in the example you gave using QTextSream you seem to be reading a file in, not writing it out. OK, so your original example should be:



#include <QtCore/QString>
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>

int main(int argc, char **argv)
{
/* Try and open a file for output */
QString outputFilename = "Results.txt";
QFile outputFile(outputFilename);
outputFile.open(QIODevice::WriteOnly);

/* Check it opened OK */
if(!outputFile.isOpen()){
qDebug() << argv[0] << "- Error, unable to open" << outputFilename << "for output";
return 1;
}

/* Point a QTextStream object at the file */
QTextStream outStream(&outputFile);

/* Write the line to the file */
outStream << "Victory!\n";

/* Close the file */
outputFile.close();
return 0;
}