PDA

View Full Version : How can write text in a file in 32bit qt installed on x64 windows 7



Pardeep
3rd October 2012, 06:16
Hi,
I am newbie in qt. i want to write text from my qt application into a file, but file isn't created. i try to print the error occured during file creation... error is " unknown error". My qt is 32 bit & i have installed it on x64 window7. i have done same thing on 32 bit windows 7 but there in no problem. so plz help me for this. Thanks in advance.

ChrisW67
3rd October 2012, 23:04
Here is what we know: you have tried to create a file and it failed... hardly a lot of information to go on.

The mostly likely causes are: the path you have used does not exist or you do not have permission to write in that location. We have no way to know.

Pardeep
4th October 2012, 05:10
It may be, but i write same code on 32 bit machine also, there is no such problem.
Below is code that i write on both machine.

QFile file("../styleSheetFile.txt");
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox *msg = new QMessageBox();

msg->setWindowTitle("Error Message");
msg->setText("Can't open file");
msg->show();
}
else
{
QTextStream out(&file);
out <<textEdit->text();
}
file.close();

ChrisW67
4th October 2012, 08:05
It may be, but i write same code on 32 bit machine also, there is no such problem.
Think about it a bit harder; if there is no such problem then it either works or you can tell me what other scenario caused the issue.

You are using a relative path, a recipe for disaster unless you tightly control the current working directory. What is the current working directory of the process? Usually, unless you do something to change it, the working directory with be "C:\Program Files(x86)\MyApp"; this directory and the one above it will not be writable to unprivileged processes. If the current working directory is the root of a drive then there is no parent directory to write into.

Windows has recommended locations for persistent application data. Some of them are locatable using QDesktopServices. You probably should use them.

BTW: Your error message box is a memory leak. You would usually put this on the stack and use exec() rather than show().

Pardeep
5th October 2012, 10:32
Thank you very much. I have created file now on 64 bit system too.Thank You again.