×
Namespaces

Variants
Actions
Revision as of 04:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to write data to a file in Qt

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to use QFile to write data to a file.

Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QFile, QTextStream,Qstring, Text Label
Created: james1980 (07 Jan 2009)
Last edited: hamishwillee (11 Oct 2012)

Overview

This application does not use a QCoreApplication object, as QFile does not need the event loop – our example is finished after sequentially working through the source code in main(). First, the example creates a file then open the file in write mode. If the file was opened correctly, a QTextStream is constructed using the QFile object (based on QIODevice). Then we write the data to the file and finally close the file.

Source File

#include <QFile>
#include <QCoreApplication>
#include <QTextStream>
 
int main(int argc, char *argv[])
{
//QCoreApplication app(argc, argv); //(we don't use these)
 
QFile file("c://out.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << "This file is generated by Qt\n";
 
// optional, as QFile destructor will already do it:
file.close();
 
//this would normally start the event loop, but is not needed for this
//minimal example:
//return app.exec();
 
return 0;
}
  • Above code will generate the file out.txt into C:\.
  • It is also important to notice that the default location for files on Symbian platform is "/private/<SID>". The driver folder is located depends on the driver the application is installed. So, a file named 'out.txt' will be located in folder '/private/<SID>/out.txt'
#include <QFile>
#include <QCoreApplication>
#include <QTextStream>
 
int main(int argc, char *argv[])
{
//QCoreApplication app(argc, argv); //(we don't use these)
 
QFile file("out.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << "This file is generated by Qt\n";
 
// optional, as QFile destructor will already do it:
file.close();
 
//this would normally start the event loop, but is not needed for this
//minimal example:
//return app.exec();
 
return 0;
}

File Read/write in meego-harmattan

For reading/writing files on meego-harmattan, HOME (/home/user) directory should basically be used:

//you might want to keep files somewhere in a folder named after your application, so create a directory in home path(let say :/local/share/YourAppName/somefolder/) 
// using QDir::mkdir & then use the code below or if not then simply keep files in HOME path
 
QString testbuf;
QString filename ="/home/user/local/share/YourAppName/somefolder/somefile.txt"
QFile file(filename);
if ( file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream( &file );
stream << testbuf << endl;
file.close();
}
 
//Similarly Read operation can also be done.
This page was last modified on 11 October 2012, at 04:17.
2228 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×