PDA

View Full Version : Cant get QFile to write (even using the QT documentation example!



mobucl
25th January 2011, 10:24
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):



#include <QtCore/QCoreApplication>
#include<QTextStream>
#include<QFile>

int main()
{
QFile file("D://out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&file);
out << "The magic number is: " << 49 << "\n";
}

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

nish
25th January 2011, 10:32
i think you need to change "D://out.text" to "D:/out.txt"
or if on windows (ofcourse) "D:\\out.txt"

Zlatomir
25th January 2011, 10:35
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:

#include <QtCore/QCoreApplication>
#include<QTextStream>
#include<QFile>

int main()
{
QFile file("D:/out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return 1; //return 1 in case the file can't be opened

QTextStream out(&file);
out << "The magic number is: " << 49 << "\n";
file.close();
return 0; //returns 0 if everything executed
}

mobucl
25th January 2011, 10:42
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!

Zlatomir
25th January 2011, 10:52
...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... )

mobucl
25th January 2011, 11:06
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!