PDA

View Full Version : Creation and writing to .dat file using QFile on Windows some problem…



freely
8th August 2011, 14:10
Hello,
Does anybody know what is wrong with next:
I need to create file in some directory with rw permissions on disk then write to it data and open after. On Windows and Linux, MacOS also.
For that I use next code:


...
QString outputDir = "some_dir"; // with "/" separators
QString fileName = "some_file_name" + ".dat";
QFile file(outputDir+"/"+fileName);
if (!file.open(QIODevice::WriteOnly|QFile::WriteOnly) )
{
QMessageBox::warning(0,"Could not create Project File",
QObject::tr( "\n Could not create Project File on disk"));

return false;
}
else
{
QDataStream stream(&file);
...
file.close();
}
...

On Linux ( Ubuntu ) it works ok ( file.open(…) returns true ) – file is created , written , closed ,reopened ,read. But when I run the same code on Windows file.open(…) returns false in any directory I choose. It is written in Qt docs that Qt automatically treats right directory path names ( separators ) for underlying OS ( translates “/” ( default ) to underlying OS ).
I’ve tried to use QDir::toNativeSeparators(outputDir+”/”+fileName) inside file.open(…) but still the file couldn’t be created on Windows – file.open(…) returns false.
***
Does anybody can help/point what is wrong with creating new file on windows in this case ? ( what is wrong with file.open(…) ) ?
How to solve so that I can open new file on Windows using QFile?

Thank you in advance.

freely
8th August 2011, 16:17
I’ve outputed :
after


QFile file(QDir::toNativeSeparators(outputDir + "/" + fileName));
file.open(QFile::ReadWrite);



I’ve get: file.fileName() = “(path to file + file name BUT with separators “/” and not “\” as on Windows)”
file.errorString() = “The parameter is incorrect”
file.error() = 5 ( Error in opening file ).

Why file.fileName() returns path name with “/” ( Linux like separators ) and not that of Windows “\\” or “\” ??
Still can’t open file in rw directory?
What is the problem?
Thank you for your help.

freely
9th August 2011, 09:37
Separators are “treated” automatically by Qt. I’ve changed file name for Windows and now it works.
Thanks for your help. Solved.