Results 1 to 6 of 6

Thread: Problem with QFile

  1. #1
    Join Date
    Mar 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problem with QFile

    Hi
    I want to put some text in a text file. If the file don't exists i want to create it.

    here is my code for this.
    Qt Code:
    1. QFile file("Output.txt");
    2. file.open(QIODevice::WriteOnly | QIODevice::Text);
    3. QTextStream out( &file );
    4.  
    5. out << StrABeskrivning << endl;
    6. out << IntAPris << endl;
    7. out << StrBBeskrivning << endl;
    8. out << IntBPris;
    To copy to clipboard, switch view to plain text mode 

    But when I compile it i get this error:
    error: variable 'QTextStream out' has initializer but incomplete type

    Do someone have a idea?

    /
    Victor

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

    Default Re: Problem with QFile

    Add this to your file:
    Qt Code:
    1. #include <QTextStream>
    To copy to clipboard, switch view to plain text mode 
    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.


  3. #3
    Join Date
    Mar 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with QFile

    Thanks for your answer.
    I still have a problem.
    When i run the program it creates no text file.
    I don't understand why.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QFile

    Quote Originally Posted by viciv919 View Post
    Thanks for your answer.
    I still have a problem.
    When i run the program it creates no text file.
    I don't understand why.
    Please close the file after finished writing data.

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

    Default Re: Problem with QFile

    Quote Originally Posted by viciv919 View Post
    Thanks for your answer.
    I still have a problem.
    When i run the program it creates no text file.
    I don't understand why.
    Maybe the file is created in a different directory than you expect (or not created at all because of lack of permissions). Please see what QFile::open() returns.
    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.


  6. #6
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QFile

    Here is an example of a header I sometimes use in text files.....the first part of the code allows the user to select where to place the file and what name they want to give the file.


    Qt Code:
    1. QString txtFile = QFileDialog::getSaveFileName(this, tr("Save Error Report"), ":",
    2. tr("Error Report (*.txt)"));
    3. QFile txtF(txtFile);
    4.  
    5. if(txtF.open(QFile::WriteOnly | QFile::Truncate))
    6. {
    7. QTextStream out(&txtF);
    8.  
    9. out << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n";
    10. out << "~ Title of whatever your outputting...\r\n";
    11. out << "~ Date: " <<tr("%1 \r\n").arg(QDateTime::currentDateTime().toString());
    12. out << "~ File Name: "<< tr("%1 \r\n").arg(yourFileName);
    13. out << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n";
    14. out << "\r\n";
    15.  
    16. txtF.close();
    To copy to clipboard, switch view to plain text mode 

    Try that and see if it helps you any!

Similar Threads

  1. Problem with QFile::atEnd() function
    By bbad68 in forum Newbie
    Replies: 5
    Last Post: 11th February 2010, 21:32
  2. Problem with QFile and copying;
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 31st October 2009, 16:32
  3. QFile::atEnd() problem?
    By lvi in forum Qt Programming
    Replies: 9
    Last Post: 6th August 2008, 12:37
  4. Problem : use QHttp get a file to QFile
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 24th May 2007, 10:58
  5. problem in writing text with QFile
    By wei243 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 14:26

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.