PDA

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



jshafferman
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:



MainWindow::MainWindow()
{
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
if(errorFile.open(QIODevice::WriteOnly)
{
// QTextStream errorLog in MainWindow.h
errorLog.setDevice(errorFile);
}
else
{
// 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 :)

wysota
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 :)