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. Opening files on Windows using std::ifstream and filename provided by QFileDialog
Forum Updated to NodeBB v4.3 + New Features

Opening files on Windows using std::ifstream and filename provided by QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.3k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by Robert Hairgrove
    #1

    Hi, I am trying to pass a filename to a 3rd party library which uses the functions in std::fstream to open the file. On Linux, this works well if I let the user choose the filename to open from QFileDialog::getOpenFileName(). On Windows, it also works, but only if there are no spaces and no extended characters in the filename.

    If the user chooses a file which is returned by QFileDialog as

    C:/Users/Jörg Mustermann/Desktop/Some Folder/File Name.csv
    

    how can I format this filename on Windows so that the external library can open it? If I tell the user to store the files in a folder with no spaces directly under the C:\ directory, it works fine. Merely replacing the "/" with the "\" directory separator doesn't seem to help.

    The Windows OS is Windows 10, and the locale is German (Switzerland).

    eyllanescE 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      std::fstream does not care about spaces in filename but you have to use \ instead / -> https://doc.qt.io/qt-5/qdir.html#toNativeSeparators

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        std::fstream does not care about spaces in filename but you have to use \ instead / -> https://doc.qt.io/qt-5/qdir.html#toNativeSeparators

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks ... sorry, but I tried to type the "\" character in my first post, and it got swallowed. It is probably more a problem with the Umlaut in the path.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please show some code.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • R Robert Hairgrove

            Hi, I am trying to pass a filename to a 3rd party library which uses the functions in std::fstream to open the file. On Linux, this works well if I let the user choose the filename to open from QFileDialog::getOpenFileName(). On Windows, it also works, but only if there are no spaces and no extended characters in the filename.

            If the user chooses a file which is returned by QFileDialog as

            C:/Users/Jörg Mustermann/Desktop/Some Folder/File Name.csv
            

            how can I format this filename on Windows so that the external library can open it? If I tell the user to store the files in a folder with no spaces directly under the C:\ directory, it works fine. Merely replacing the "/" with the "\" directory separator doesn't seem to help.

            The Windows OS is Windows 10, and the locale is German (Switzerland).

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @Robert-Hairgrove try with:

            QString filename = QFileDialog::getOpenFileName(...);
            QString nativeFilename = QDir::toNativeSeparators(filename);
            

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Robert Hairgrove
              wrote on last edited by
              #6

              Thanks for the suggestions, everyone. I was able to pinpoint the issue a little better in the meantime:

              (1) You can have spaces in the path, but not extended characters such as "äöü";
              (2) Windows 10 using std::ifstream::open() seems equally happy with "/" as well as "\".

              In other words, both of these work OK, at least on Windows 10:

              C:/Users/Robert Hairgrove/Desktop/Some Folder/Some File.csv
              C:\Users\Robert Hairgrove\Desktop\Some Folder\Some File.csv
              

              But this doesn't:

              C:\Users\Jörg Mustermann\Desktop\Some Folder\Some File.csv
              

              I tried converting the filename with QString::toLocal8Bit() before passing it to the library, but in the debugger I could see that the "ö" was not converted correctly. And passing the filename directly (the library function calls for const std::string &) as UTF-8 does not work, either.

              Any other suggestions?

              Christian EhrlicherC 1 Reply Last reply
              0
              • R Robert Hairgrove

                Thanks for the suggestions, everyone. I was able to pinpoint the issue a little better in the meantime:

                (1) You can have spaces in the path, but not extended characters such as "äöü";
                (2) Windows 10 using std::ifstream::open() seems equally happy with "/" as well as "\".

                In other words, both of these work OK, at least on Windows 10:

                C:/Users/Robert Hairgrove/Desktop/Some Folder/Some File.csv
                C:\Users\Robert Hairgrove\Desktop\Some Folder\Some File.csv
                

                But this doesn't:

                C:\Users\Jörg Mustermann\Desktop\Some Folder\Some File.csv
                

                I tried converting the filename with QString::toLocal8Bit() before passing it to the library, but in the debugger I could see that the "ö" was not converted correctly. And passing the filename directly (the library function calls for const std::string &) as UTF-8 does not work, either.

                Any other suggestions?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Robert-Hairgrove said in Opening files on Windows using std::ifstream and filename provided by QFileDialog:

                I could see that the "ö" was not converted correctly.

                It was for sure... Qt does not do the conversion wrong.
                Use QFile instead ifstream.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                R 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Robert-Hairgrove said in Opening files on Windows using std::ifstream and filename provided by QFileDialog:

                  I could see that the "ö" was not converted correctly.

                  It was for sure... Qt does not do the conversion wrong.
                  Use QFile instead ifstream.

                  R Offline
                  R Offline
                  Robert Hairgrove
                  wrote on last edited by Robert Hairgrove
                  #8

                  @Christian-Ehrlicher I cannot use QFile because I am passing the filename to a 3rd party library which calls std::ifstream functions (perhaps you did not read my original message thoroughly?)

                  Calling QString::toLocal8Bit() will convert "ö" etc. to UTF-8 on Linux correctly, but not on Windows ... these Unicode characters show up in the debugger of Qt Creator as illegal Unicode characters, at least on my system.

                  I found this, and it looks like I am stuck if the user insists on saving the input files in a path containing extended characters:
                  https://newbedev.com/how-to-open-an-std-fstream-ofstream-or-ifstream-with-a-unicode-filename

                  J.HilkJ Christian EhrlicherC 2 Replies Last reply
                  0
                  • R Offline
                    R Offline
                    Robert Hairgrove
                    wrote on last edited by Robert Hairgrove
                    #9

                    I also noticed that using "/" as directory separator has been working fine with std::fstream under all versions of Windows for a long time (at least since 2009, I believe).

                    1 Reply Last reply
                    0
                    • R Robert Hairgrove

                      @Christian-Ehrlicher I cannot use QFile because I am passing the filename to a 3rd party library which calls std::ifstream functions (perhaps you did not read my original message thoroughly?)

                      Calling QString::toLocal8Bit() will convert "ö" etc. to UTF-8 on Linux correctly, but not on Windows ... these Unicode characters show up in the debugger of Qt Creator as illegal Unicode characters, at least on my system.

                      I found this, and it looks like I am stuck if the user insists on saving the input files in a path containing extended characters:
                      https://newbedev.com/how-to-open-an-std-fstream-ofstream-or-ifstream-with-a-unicode-filename

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @Robert-Hairgrove I don't think you will get that working, without modifying the dll.

                      As a last resort, you could use QFile and QStandardPaths and copy the file to a temp/appdata location and pass the new simplified path to the dll


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      R 1 Reply Last reply
                      1
                      • R Robert Hairgrove

                        @Christian-Ehrlicher I cannot use QFile because I am passing the filename to a 3rd party library which calls std::ifstream functions (perhaps you did not read my original message thoroughly?)

                        Calling QString::toLocal8Bit() will convert "ö" etc. to UTF-8 on Linux correctly, but not on Windows ... these Unicode characters show up in the debugger of Qt Creator as illegal Unicode characters, at least on my system.

                        I found this, and it looks like I am stuck if the user insists on saving the input files in a path containing extended characters:
                        https://newbedev.com/how-to-open-an-std-fstream-ofstream-or-ifstream-with-a-unicode-filename

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Robert-Hairgrove said in Opening files on Windows using std::ifstream and filename provided by QFileDialog:

                        these Unicode characters show up in the debugger of Qt Creator as illegal Unicode characters, at least on my system.

                        Because it's not utf-8 but your current locale.

                        There is nothing Qt can do here - fix your library to properly handle non-latin characters in the current locale or in utf-8 as @J-Hilk already said.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @Robert-Hairgrove I don't think you will get that working, without modifying the dll.

                          As a last resort, you could use QFile and QStandardPaths and copy the file to a temp/appdata location and pass the new simplified path to the dll

                          R Offline
                          R Offline
                          Robert Hairgrove
                          wrote on last edited by
                          #12

                          @J-Hilk Thank you. Fortunately, this is a source-code library, so no DLL to worry about.

                          There might be an equivalent function taking a memory buffer I could use, in which case I could let QFile open the file, as suggested by @Christian-Ehrlicher.

                          Otherwise, copying the file to a non-Unicode path seems to be the only way.

                          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