Results 1 to 5 of 5

Thread: i can write to a file using QFile, but can not do it using QFile *, why?

  1. #1
    Join Date
    Apr 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default i can write to a file using QFile, but can not do it using QFile *, why?

    hello, i was creating a basic and create a private QFile * and QTextStream * in my class because i wanted to use them inside slots, but i realized that i was able to open, read and even create files but was not able to write to them, after some tests i decided to use a QFile inside every slot (read the file and write contents into a QTextEdit when a button is pressed, save the file when other button is clicked, etc) that is just used inside the function and then deleted, and i were able to write to files using the QFile, but not the QFile *, i will contine using QFile but i want to know why i cant write to files using a QFile *, thanks

  2. #2
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: i can write to a file using QFile, but can not do it using QFile *, why?

    If you use QFile*, you're declaring a pointer, you need to use "new QFile()" to it works.
    And open it with the option for writing ^^

  3. #3
    Join Date
    Apr 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: i can write to a file using QFile, but can not do it using QFile *, why?

    yes, i know it is a pointer, i used f->open(QFile:;WriteOnly) to open the file and declared the text stream to write, but it doesnt work, i have to use a QFile instead of QFile * because it doesnt write content


    Added after 4 minutes:


    ... and file->error() returns QFile:;NoError
    Last edited by htroyo; 9th May 2011 at 04:15.

  4. #4
    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: i can write to a file using QFile, but can not do it using QFile *, why?

    Works fine:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8.  
    9. QFile *outFile = new QFile("out.txt");
    10. if (outFile->open(QIODevice::WriteOnly)) {
    11. outFile->write("See, it does get out!\n");
    12. outFile->close();
    13. }
    14. delete outFile;
    15.  
    16. QFile *inFile = new QFile("out.txt");
    17. if (inFile->open(QIODevice::ReadOnly)) {
    18. QByteArray line = inFile->readLine();
    19. inFile->close();
    20. qDebug() << line;
    21. }
    22. delete inFile;
    23.  
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 
    gives
    Qt Code:
    1. "See, it does get out!
    2. "
    To copy to clipboard, switch view to plain text mode 

    So, the question becomes what are you doing wrong? Are you attempting to write a file somewhere you do not have permission (e.g. under Program Files on Windows)? Are you not writing or reading the file you think you are?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: i can write to a file using QFile, but can not do it using QFile *, why?

    yes, i know it is a pointer, i used f->open(QFile:;WriteOnly) to open the file and declared the text stream to write,
    ... and file->error() returns QFile:;NoError
    It looks like you didn't initialize the pointer as rsilva noted:
    you need to use "new QFile()" to it works.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. How to write in a QFile
    By gorka_sm in forum Newbie
    Replies: 2
    Last Post: 5th May 2011, 12:08
  2. Read and Write file to/from QFile
    By ruben.rodrigues in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2011, 14:57
  3. Replies: 1
    Last Post: 11th October 2010, 10:21
  4. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  5. Unable to write to file QFile
    By cuter in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 11:19

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.