View Full Version : Creating a new File using QFile and QTextStream

7th September 2011, 16:12
I am trying to create an errorLog that will write out information to a .txt file so if the user has any errors while the application is running they will have record of what errors the application was reporting. The problem I am having is that I can not seem to get QFile to create a new file and write to it using a QTextStream. Here is what I am trying to do currently:

QString newFilename = QDate::currentTime().toString("ddMMyyyy") + '-' + QTime::currentTime().toString("hh::mm::ss") + "-errorLog.txt";

// QFile *errorFile in MainWindow.h
errorFile = new QFile(newFilename);

// I thought this created a new file if it wasn't available, and as far as my understanding it should create it in the same directory as the executable
// QTextStream errorLog in MainWindow.h
// it always jumps to the else clause and I am confused as to why?
cout << "Cannot open file for writing: " << qPrintable(errorFile->errorString()) << endl;

// not relevant code

The cout statement produces this: Cannot open file for writing: The parameter is in correct. I know I am whiffing on something just not sure...

Does it have to do with the ':' in the time?

Thanks for any help
The error I get back when I write t

Well probably should have checked it first but I got it, it was the ':' derrrrr!!! I realized that as soon as I typed the question in :)

7th September 2011, 17:20
Check directory permissions. Also your file may contain forbidden characters (e.g. ':' is forbidden on NTFS).

Ah... didn't see your edit :)