Results 1 to 9 of 9

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

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Where's my wrong?

    I think there's a line of code missing...
    Your "in" object is a QTextStream, right ?

    You have to link it to your file like this

    QTextStream in(&file);

    This must be done after your "file.open()" test.

    EDIT :
    OK, you corrected...
    You don't need to use a QTextStream by the way, you can do this directly :

    QString configFileContent = file.readAll();

    Guilugi.

  3. #3
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where's my wrong?

    I've tried both methods (QFile's readAll() and QTextStream's readAll() ) but i cannot get the content of the files. But I know that there is some lines in the file.. Cannot find the mistake

  4. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    If you output the result of file.size(), is it null ?
    Maybe Qt can't open it (bad permissions, wrong path...) ?

  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: Where's my wrong?(Related to QFile & QTextStream)

    Try opening without the append mode.

  6. The following user says thank you to wysota for this useful post:

    musaulker (27th March 2007)

  7. #6
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

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

    Quote Originally Posted by guilugi View Post
    If you output the result of file.size(), is it null ?
    Maybe Qt can't open it (bad permissions, wrong path...) ?
    I logged in as Administrator on Windows XP, and there's no file permission problem. Also if there is permission problem, than i couldnt write on these files. But i can (it doesnt search but it appends. very strange)

    Qt Code:
    1. qint64 fileSize = file.size(); // 3651
    2. QString configFileContent = file.readAll(); // 0
    3. int configFileSize = configFileContent.count(); // 0
    To copy to clipboard, switch view to plain text mode 

    What do you think now?

  8. #7
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

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

    Quote Originally Posted by wysota View Post
    Try opening without the append mode.
    Yes it opend, searched and writed successfully. I've thought that if I need to append it end of the file, then I must use QIODevice::Append flag..

    Thank you very much wysota and guilugi for your answers..

  9. #8
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    Well, I missed the append flag...this flag set the textstream at the end of the file...so when you read, there's nothing more to read

    Fine if it works

  10. #9
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

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

    Oh I get the point

    Thanks again

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.