Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Creation and writing to .dat file using QFile on Windows some problem...
QtWS25 Last Chance

[SOLVED] Creation and writing to .dat file using QFile on Windows some problem...

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 14.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swhweng
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      The reason of the error should be indicated by QFile::errorString.

      And QIODevice::WriteOnly and QFile::WriteOnly have the same value because the enum is inherited.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p91paul
        wrote on last edited by
        #3

        use this
        @if (!file.open(QIODevice::WriteOnly))@

        since WriteOnly is the same in QFile and QIODevice since is simply inherited, so it's useless to or those values. using QFile::WriteOnly or QIODevice::WriteOnly is exactly the same.
        probably best (but I don't know if you use text files) is to use QIODevice::WriteOnly|QIODevice::Text, that removes the problem of handling windows line terminators \r\n, that can be treaten as they are \n, also in writing(when flushing to the actual file, \n will be converted in the os default line terminator)

        you can use "error()":http://doc.qt.nokia.com/latest/qfile.html#error to retrieve last error; it will be one of "FileError":http://doc.qt.nokia.com/latest/qfile.html#FileError-enum

        you can also use "fileName()":http://doc.qt.nokia.com/latest/qfile.html#fileName to retrieve the name set (it can have double slash if outputDir already has a terminal /)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          swhweng
          wrote on last edited by
          #4

          alexisdm, p91paul you've added useful advices:
          I've outputed :
          after
          @....
          QFile file(QDir::toNativeSeparators(outputDir + "/" + fileName));
          file.open(QFile::ReadWrite);
          ...
          @
          I've got: 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.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p91paul
            wrote on last edited by
            #5

            do not use toNativeSeparators, it is probably not a good idea since QFile expects /

            I think the problem is the directory. try to copy outputDir contents to the windows shell and see what you get. probably something about windows drive letters? are you using absolute paths? or relative paths starting with /?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              swhweng
              wrote on last edited by
              #6

              without using QDir::toNativeSeparators(...) file.fileName() returns file name with path with "/" slashes that is not for Windows.
              Next also does not work:
              @
              ...
              QFile file(outputDir_+"/"+fileName);
              if (!file.open(QFile::WriteOnly))
              {
              QMessageBox::warning(0,"Could not create Project File",
              QObject::tr( "\n Could not create Project File on disk"));

                            return false;
                   }
              

              else
              ...
              @
              file.fileName() returns on Windows path and file name with "/" separators.
              How to set file name with windows separators "" ?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p91paul
                wrote on last edited by
                #7

                it's correct that fileName() contains /, because conversion is done only when actually opening the file. trust Qt and remove toNativeSeparators, is not your problem.

                you haven't replyed to my questions. are you using relative or absolute path? if relative, remember that the first char must not be /
                if absolute, on windows you have to put drive letter name; if you use / as the first char in a windows absolute path, the current drive is used.

                Another thing: if you use relative path, pay attention to the current path; It may change depending on where the executable is built and other things. try to output "QDir::currentPath()":http://doc.qt.nokia.com/latest/qdir.html#currentPath to obtain your current path, and see if it is what you expect.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Just use "/" separators for all QFile-related paths. The QFile class is smart enough to make the substitution for you. "/" is the Qt file separator, which allows you to write paths without having to worry about the native path separators in your code.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    swhweng
                    wrote on last edited by
                    #9

                    outputDir inside QFile file(outputDir_ + "/" + fileName) contains path name ( file directory ) returned from QFileDialog with "/" separators.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      swhweng
                      wrote on last edited by
                      #10

                      outputDir is absolute path that contains drive letter and "/" separators and all that passed to QFile constructor but file is not created and file.fileName() returns fileName + outputDir with the "/" separators like Qt did not take care of conversion but should.
                      I try to find the possible miss here ( solution ) , if you have time help me.
                      Thank you.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mlong
                        wrote on last edited by
                        #11

                        What are the contents of outputDir_ and fileName that you're passing to the constructor, and what is the value you're getting back from file.fileName()?

                        What are you expecting that you should see?

                        Software Engineer
                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          swhweng
                          wrote on last edited by
                          #12

                          mlong please read my previous post I've wrote there what is in outputDir ( directory name with "/" separators ) and there is some windows friendly file name with .dat extension in fileName. That passed to the QFile constructor and I expect separators are automatically converted to "" for Windows but as you can see they didn't. So I'm looking what is the problem...

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mlong
                            wrote on last edited by
                            #13

                            [quote author="Pavel Mazniker" date="1312828520"]mlong please read my previous post I've wrote there ...[/quote]

                            I realize that. I was just looking for a more concrete example "E:/foo/bar/baz/file.dat" or whatever... just to make sure that there's not something else subtle that is happening in your use case.

                            I believe that file.fileName() will not return the "" separators. It will continue to use the Qt-friendly "/" separators. The "/" and "" conversion happens at a lower level.

                            Software Engineer
                            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              swhweng
                              wrote on last edited by
                              #14

                              why the file is not opened(created) on Windows in the case?

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mlong
                                wrote on last edited by
                                #15

                                Without more information, it's hard to tell. Qt's pretty robust in opening files, so I would be led to believe that there's more than likely something subtle in the way the file and/or path is being set up. Could you create and post a small example snippet of code (very similar to your first posts's code sample) which has a real path and filename which is not working?

                                Software Engineer
                                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  goetz
                                  wrote on last edited by
                                  #16

                                  Pavel, it's time to post some concrete examples. Your "anonymized" snippets are hard to analyze. Some actual paths, as mlong suggested, would be very helpful.

                                  Qt does its job with files pretty good. So it is very likely that you messed up some paths.

                                  Double check the actual paths with [[Doc:QFileInfo]]:

                                  @
                                  QFileInfo fi(file);
                                  qDebug() << fi.absoluteFilePath();
                                  @

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    swhweng
                                    wrote on last edited by
                                    #17

                                    Separators are "treated" automatically by Qt. I've changed file name for Windows and now it works.
                                    Thanks for your help. Solved.

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mlong
                                      wrote on last edited by
                                      #18

                                      Good deal! Please be sure and add [SOLVED] to the beginning of the thread title! Thanks!

                                      Software Engineer
                                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved