Results 1 to 9 of 9

Thread: Where's my wrong?(Related to QFile & QTextStream)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Where's my wrong?(Related to QFile & QTextStream)

    Hello,

    I'm a newbie on programming QT. I have a problem, please look at the details;

    I want to search in a file and if there exists such a text "foo bar" then i'll break operation, if it does not exists i'll append it to the file. Here's my code:

    Qt Code:
    1. QFile file("c:\\abc.cfg");
    2. if(!file.exists())
    3. {
    4. return false;
    5. }
    6. if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
    7. {
    8. return false;
    9. }
    10. QString configFileContent = file.readAll();
    11. if (!configFileContent.contains("foo bar"))
    12. {
    13. QTextStream out(&file);
    14. out << "foo bar\n";
    15. }
    16. file.close();
    To copy to clipboard, switch view to plain text mode 

    The problem is, it opens file but i cannot get the content of the file. After that it thinks foo bar doesnt exists so it writes at the end of the file. But foo bar is there.

    Why did it cannot get the content of the file and search foo bar? Where's the problem? How can i solve it?

    Your regards,

    PS: Also i tried this code too, but i have the same problem..
    Qt Code:
    1. QFile file("c:\\abc.cfg");
    2. if(!file.exists())
    3. {
    4. return false;
    5. }
    6.  
    7. if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
    8. {
    9. return false;
    10. }
    11. QTextStream in(&file);
    12. QString configFileContent = in.readAll();
    13. if (!configFileContent.contains("foo bar"))
    14. {
    15. QTextStream out(&file);
    16. out << "foo bar\n";
    17. }
    18. file.close();
    To copy to clipboard, switch view to plain text mode 

    EDIT: Corrected missing part (in changed to file). Sorry about the mistake
    Last edited by musaulker; 27th March 2007 at 09:44. Reason: updated title & content

Similar Threads

  1. Replies: 7
    Last Post: 17th July 2009, 09:40
  2. What's wrong with my actions???
    By fullmetalcoder in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2007, 18:49
  3. Qt renders wrong font
    By durbrak in forum Qt Programming
    Replies: 8
    Last Post: 2nd November 2006, 14:36
  4. QListWidget...what's wrong
    By nupul in forum Newbie
    Replies: 16
    Last Post: 4th April 2006, 12:17
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06

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.