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
);
/* 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 */
/* Write the line to the file */
outStream << "Victory!\n";
/* Close the file */
outputFile.close();
return 0;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks