Results 1 to 8 of 8

Thread: writing to a text-file

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default writing to a text-file

    I have made a simple qt4-app with Carbide ide.
    In one of my functions i am trying to write some text to a text-file but it is not working. I added file name result.txt to the project in Carbide.

    Here is the code:
    Qt Code:
    1. QFile file("result.txt");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file); // we will serialize the data into the file
    4. out << QString("victory"); // serialize a string
    To copy to clipboard, switch view to plain text mode 

    I also have included <QFile> and <QDataStream> in cpp-file.

    The program dosent show any errors but it dosent write anythink to the file.

  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: writing to a text-file

    What does QFile::open() return?

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: writing to a text-file

    I tried to use debbugger to check the return value but there was just a lot of variables and values like QIODevice- folder->QObject->d_ptr->d = 0x424cc19c
    and
    QIODevice- folder->QObject->d_ptr->d->unused=3651511 so from those i dont know..

    i added this code in the function:

    Qt Code:
    1. QFile file("result.txt");
    2. if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. {
    4. QMessageBox msgBox;
    5. msgBox.setText("File opened");
    6. msgBox.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    And i did get the message. So the problem aint in opening the text-file?
    I am running the program in windows xp.

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: writing to a text-file

    Have you tried
    Qt Code:
    1. QFile file;
    2. file.open("result.txt", QIODevice::WriteOnly);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: writing to a text-file

    i got the following error: function call '[QFile].open({lval} const char[9], QIODevice::OpenModeFlag)' does not match

    i also chaged the messagebox code to this:

    Qt Code:
    1. QTextStream in(&file);
    2. while (!in.atEnd()) {
    3. QString line = in.readLine();
    4. }
    To copy to clipboard, switch view to plain text mode 

    In debugger values for example. line-d->data= Can't read memory from address 0xCCCCCCDC

  6. #6
    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: writing to a text-file

    Quote Originally Posted by QtBros61 View Post
    And i did get the message. So the problem aint in opening the text-file?
    But you checked it for reading, not for writing.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: writing to a text-file

    As an aside, do you want to write a human readable text file or a binary file that contains some text? Use QTextStream and QDataStream respectively. Your first post used QDataStream and your last post used QTextSTream, which is a recipe for confusion.

  8. #8
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: writing to a text-file

    ChrisW67 is correct. To write text, you should use QTextSream. Although, in the example you gave using QTextSream you seem to be reading a file in, not writing it out. OK, so your original example should be:

    Qt Code:
    1. #include <QtCore/QString>
    2. #include <QtCore/QFile>
    3. #include <QtCore/QDebug>
    4. #include <QtCore/QTextStream>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. /* Try and open a file for output */
    9. QString outputFilename = "Results.txt";
    10. QFile outputFile(outputFilename);
    11. outputFile.open(QIODevice::WriteOnly);
    12.  
    13. /* Check it opened OK */
    14. if(!outputFile.isOpen()){
    15. qDebug() << argv[0] << "- Error, unable to open" << outputFilename << "for output";
    16. return 1;
    17. }
    18.  
    19. /* Point a QTextStream object at the file */
    20. QTextStream outStream(&outputFile);
    21.  
    22. /* Write the line to the file */
    23. outStream << "Victory!\n";
    24.  
    25. /* Close the file */
    26. outputFile.close();
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Writing raw bytes as text?
    By DiamonDogX in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 17:56
  2. problem in writing text with QFile
    By wei243 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 15:26
  3. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 17:23
  4. Writing Text on a Rectangle!!!
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2006, 11:23
  5. Writing an XML file
    By qball2k5 in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2006, 11:58

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.