Results 1 to 4 of 4

Thread: Problems writing to file

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Problems writing to file

    I have a logging class that I wish to use to append messages to a file. Naturally, I wrote a write(QString msg) for my class that I wish clients to be able to call in order to append a message to the current log file. But I can't seem to successfully write to the file... it seems to have a problem with how I am using my member variables. I have successfully written to a file like this, but only when I open it and write to it in one block of code (like how you see here: http://www.digitalfanatics.org/proje...chapter08.html). Here's my code that isn't working (_fileTextStream is a member variable of type QTextStream):

    Qt Code:
    1. Logger::Logger()
    2. {
    3. QFile file( "log.txt" );
    4. if ( !file.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) )
    5. {
    6. QMessageBox::critical( NULL,
    7. tr("Error opening log file for writing."),
    8. tr("Couldn't open file: %1").arg( file.errorString() ) );
    9. }
    10.  
    11. QTextStream _fileTextStream( &file );
    12. }
    13.  
    14.  
    15. void Logger::write( QString message )
    16. {
    17. _fileTextStream << message << endl;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 24th April 2009 at 15:17. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems writing to file

    Quote Originally Posted by DiamonDogX View Post
    Qt Code:
    1. Logger::Logger()
    2. {
    3. QFile file( "log.txt" );
    4. if ( !file.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) )
    5. {
    6. QMessageBox::critical( NULL,
    7. tr("Error opening log file for writing."),
    8. tr("Couldn't open file: %1").arg( file.errorString() ) );
    9. }
    10.  
    11. QTextStream _fileTextStream( &file );
    12. }
    13.  
    14.  
    15. void Logger::write( QString message )
    16. {
    17. _fileTextStream << message << endl;
    18. }
    To copy to clipboard, switch view to plain text mode 
    In that code _fileTextStream is only a local variable and therefore your code is not working. Make _fileTextStream a real member variable and use QTextStream::setDevice().

    Edit:
    Qt Code:
    1. class Logger
    2. {
    3. //....
    4. private:
    5. QTextStream _fileTextStream;
    6. }
    7.  
    8. Logger::Logger()
    9. {
    10. //..
    11. _fileTextStream.setDevice(&file);
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Problems writing to file

    That did not seem to fix the problem... the file is still not written to.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems writing to file

    The problem is QFile is destroyed when constructor terminate.
    QFile automatically close the file controlled when the object is destroyed
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 14
    Last Post: 16th March 2009, 09:19
  2. Writing Log Records To A Log File
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2007, 13:08
  3. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 16:23
  4. Writing to file at specific
    By safknw in forum Qt Programming
    Replies: 3
    Last Post: 1st December 2006, 11:12
  5. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.