Results 1 to 2 of 2

Thread: Creating a new File using QFile and QTextStream

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    13

    Default Re: Creating a new File using QFile and QTextStream

    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:

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QString newFilename = QDate::currentTime().toString("ddMMyyyy") + '-' + QTime::currentTime().toString("hh::mm::ss") + "-errorLog.txt";
    4.  
    5. // QFile *errorFile in MainWindow.h
    6. errorFile = new QFile(newFilename);
    7.  
    8. // 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
    9. if(errorFile.open(QIODevice::WriteOnly)
    10. {
    11. // QTextStream errorLog in MainWindow.h
    12. errorLog.setDevice(errorFile);
    13. }
    14. else
    15. {
    16. // it always jumps to the else clause and I am confused as to why?
    17. cout << "Cannot open file for writing: " << qPrintable(errorFile->errorString()) << endl;
    18. }
    19.  
    20. // not relevant code
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Creating a new File using QFile and QTextStream

    Check directory permissions. Also your file may contain forbidden characters (e.g. ':' is forbidden on NTFS).

    Ah... didn't see your edit
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 27th May 2009, 13:49
  2. QFile and QTextStream problems
    By uchennaanyanwu in forum Newbie
    Replies: 0
    Last Post: 7th August 2008, 03:44
  3. QFile, QTextStream
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 12th January 2008, 20:40
  4. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2006, 00:12
  5. Qfile and QTextStream
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2006, 11:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.