Results 1 to 3 of 3

Thread: Creation and writing to .dat file using QFile on Windows some problem…

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:

    Qt Code:
    1. ...
    2. QString outputDir = "some_dir"; // with "/" separators
    3. QString fileName = "some_file_name" + ".dat";
    4. QFile file(outputDir+"/"+fileName);
    5. if (!file.open(QIODevice::WriteOnly|QFile::WriteOnly))
    6. {
    7. QMessageBox::warning(0,"Could not create Project File",
    8. QObject::tr( "\n Could not create Project File on disk"));
    9.  
    10. return false;
    11. }
    12. else
    13. {
    14. QDataStream stream(&file);
    15. ...
    16. file.close();
    17. }
    18. ...
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Creation and writing to .dat file using QFile on Windows some problem…

    I’ve outputed :
    after

    Qt Code:
    1. QFile file(QDir::toNativeSeparators(outputDir + "/" + fileName));
    2. file.open(QFile::ReadWrite);
    To copy to clipboard, switch view to plain text mode 


    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.

  3. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.

Similar Threads

  1. Replies: 4
    Last Post: 9th May 2011, 09:52
  2. QFile resized files gets truncated after writing
    By MaximA in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 17:23
  3. Windows file handle on QFile?
    By Teerayoot in forum Qt Programming
    Replies: 4
    Last Post: 14th May 2007, 11:11
  4. problem in writing text with QFile
    By wei243 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 14:26

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.