Creation and writing to .dat file using QFile on Windows some problem…
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:
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
{
...
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.
Re: Creation and writing to .dat file using QFile on Windows some problem…
I’ve outputed :
after
Code:
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.
Re: Creation and writing to .dat file using QFile on Windows some problem…
Separators are “treated†automatically by Qt. I’ve changed file name for Windows and now it works.
Thanks for your help. Solved.