PDA

View Full Version : Problem with QFile



viciv919
13th March 2010, 07:50
Hi
I want to put some text in a text file. If the file don't exists i want to create it.

here is my code for this.


QFile file("Output.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out( &file );

out << StrABeskrivning << endl;
out << IntAPris << endl;
out << StrBBeskrivning << endl;
out << IntBPris;


But when I compile it i get this error:
error: variable 'QTextStream out' has initializer but incomplete type

Do someone have a idea?

/
Victor

wysota
13th March 2010, 08:22
Add this to your file:

#include <QTextStream>

viciv919
13th March 2010, 09:24
Thanks for your answer.
I still have a problem.
When i run the program it creates no text file.
I don't understand why.

yogeshgokul
13th March 2010, 10:20
Thanks for your answer.
I still have a problem.
When i run the program it creates no text file.
I don't understand why.

Please close the file after finished writing data.

wysota
13th March 2010, 13:37
Thanks for your answer.
I still have a problem.
When i run the program it creates no text file.
I don't understand why.

Maybe the file is created in a different directory than you expect (or not created at all because of lack of permissions). Please see what QFile::open() returns.

progman
17th March 2010, 16:04
Here is an example of a header I sometimes use in text files.....the first part of the code allows the user to select where to place the file and what name they want to give the file.




QString txtFile = QFileDialog::getSaveFileName(this, tr("Save Error Report"), ":",
tr("Error Report (*.txt)"));
QFile txtF(txtFile);

if(txtF.open(QFile::WriteOnly | QFile::Truncate))
{
QTextStream out(&txtF);

out << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n";
out << "~ Title of whatever your outputting...\r\n";
out << "~ Date: " <<tr("%1 \r\n").arg(QDateTime::currentDateTime().toString());
out << "~ File Name: "<< tr("%1 \r\n").arg(yourFileName);
out << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n";
out << "\r\n";

txtF.close();



Try that and see if it helps you any!