I am trying to write into a file and if the file doesn\'t exist create it. I have searched on the internet and nothing worked for me.
My code looks currently like this:<
That is weird, everything looks fine, are you sure it does not work for you? Because this main
surely works for me, so I would look somewhere else for the source of your problem.
#include
#include
int main()
{
QString filename = "Data.txt";
QFile file(filename);
if (file.open(QIODevice::ReadWrite)) {
QTextStream stream(&file);
stream << "something" << endl;
}
}
The code you provided is also almost the same as the one provided in detailed description of QTextStream so I am pretty sure, that the problem is elsewhere :)
Also note, that the file is not called Data
but Data.txt
and should be created/located in the directory from which the program was run (not necessarily the one where the executable is located).