Results 1 to 6 of 6

Thread: Cant get QFile to write (even using the QT documentation example!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cant get QFile to write (even using the QT documentation example!

    Hi, I need to write some text to a file and i am trying to use QFile however i have a few problems, first i thought it was my code so i created a new project with just the example given in the QT documentation and i cant even get that to work! So using the example (where i replace out.txt with D://out.txt to write to my D drive):

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include<QTextStream>
    3. #include<QFile>
    4.  
    5. int main()
    6. {
    7. QFile file("D://out.txt");
    8. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    9. return;
    10.  
    11. QTextStream out(&file);
    12. out << "The magic number is: " << 49 << "\n";
    13. }
    To copy to clipboard, switch view to plain text mode 

    first I get the error:

    ..\filewrite\main.cpp:10: error: return-statement with no value, in function returning 'int'

    If i comment out the if statement it runs but i don't create a file. i thought maybe i need to close the file first so i added file.close but that didn't do anything either.

    Can someone please help!

    Thanks

    Matt

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cant get QFile to write (even using the QT documentation example!

    i think you need to change "D://out.text" to "D:/out.txt"
    or if on windows (ofcourse) "D:\\out.txt"

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Cant get QFile to write (even using the QT documentation example!

    Your OS doesn't like the path: "D://out.txt"
    So use QFile file("D:/out.txt"); //it works on windows too

    Also you say to the compiler your main function returns an int, so put that to use:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include<QTextStream>
    3. #include<QFile>
    4.  
    5. int main()
    6. {
    7. QFile file("D:/out.txt");
    8. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    9. return 1; //return 1 in case the file can't be opened
    10.  
    11. QTextStream out(&file);
    12. out << "The magic number is: " << 49 << "\n";
    13. file.close();
    14. return 0; //returns 0 if everything executed
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cant get QFile to write (even using the QT documentation example!

    Thanks for both your replies yes i notice in the documentation that "QFile expects the file separator to be '/' regardless of operating system. The use of other separators (e.g., '\') is not supported."

    Following Zlatomir's code solved the problem so thanks for that! However i still wonder why the actual example in the QT documentation doent work (i mean the return; causing an error).

    Thanks for you advice!

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Cant get QFile to write (even using the QT documentation example!

    Quote Originally Posted by mobucl View Post
    ...However i still wonder why the actual example in the QT documentation doent work (i mean the return; causing an error).
    Because you wrote (and you should wrote that way, this is standard):
    int main() {
    //...
    return some_int; //you need this line to return some int from the function main.
    }
    Value 0 tells the OS that your application executed normally and any other value means some error.

    You can have multiple return statements if the execution can go on different paths (just like the example), but be carefull not to return before you need.
    Example: Go to the out.txt right-click -> Properties -> check read only -> Ok
    Then execute again the application... see what value the app is returning (in Qt Creator -> Application Output you will have YOUR.exe exited with code... )

  6. #6
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cant get QFile to write (even using the QT documentation example!

    Ok i see! thanks Zlatomir, i will try the example when i get a sec, i guess the code wont be zero as it couldnt write the file? I will check this when i have a second! thanks alot!

Similar Threads

  1. QFile write isn't stable
    By ruben.rodrigues in forum Newbie
    Replies: 2
    Last Post: 8th July 2010, 21:14
  2. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  3. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  4. possible open/write two files same time with QFile
    By npotency in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2009, 00:28
  5. Unable to write to file QFile
    By cuter in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 11:19

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.