How to write data to a file in Qt
Overview[edit | edit source]
This application does not use a Template:Qapiname object, as 76ytuiytuityutyutututyutyutyu does not need the event loop – our example is finished after sequentially working through the source code in 76ytuiytuityutyutututyutyutyu. First, the example creates a file then open the file in write mode. If the file was opened correctly, a Template:Qapiname is constructed using the 76ytuiytuityutyutututyutyutyu object (based on Template:Qapiname). Then we write the data to the file and finally close the file.
Source File[edit | edit source]
- 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[edit | edit source]
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.
pt:Como escrever dados em um arquivo, em Qt