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. QT muggle not able to open file with c++ fstream. Please give me a hand.

QT muggle not able to open file with c++ fstream. Please give me a hand.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 666 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.
  • M Offline
    M Offline
    muggleR
    wrote on 18 May 2022, 03:29 last edited by
    #1

    I'm Currently working on project need to read from a file using c++ fstream


    Environment: Qt version :community 6.3 on windows10

    1. this is my resouce editor
      d2f3dbd7-a350-4dc9-ac20-083d10456b6a-image.png
    2. This is the code to open the file
     ifstream reader(":/data/data/Users.txt", ios_base::in);
            if (!reader.is_open())
            {
                cout << "Not able to open Users.txt" << endl;
                exit(2);
            }
    

    and the output

    Not able to open Users.txt
    Debugging V1.exe has finished with exit code 2.
    
    1. and I got the data.txt path from here
      b291cc7a-a8b9-494c-9877-7456ec34d3df-image.png

    I'd appreciate it if you could offer me some advice, many thanks.

    J 1 Reply Last reply 18 May 2022, 08:16
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 18 May 2022, 07:54 last edited by
      #2

      You are trying to use a Qt embedded resource from a non-Qt function that is not aware of them. The path provided to the ifstream constructor is being interpreted directly against the Windows file system, and no such file exists. The QFile equivalent knows about the Qt resource system and, when it sees the ":" prefix in the path, switches to looking in the compiled-in resources rather than the host file system.

      M 1 Reply Last reply 18 May 2022, 08:23
      5
      • M muggleR
        18 May 2022, 03:29

        I'm Currently working on project need to read from a file using c++ fstream


        Environment: Qt version :community 6.3 on windows10

        1. this is my resouce editor
          d2f3dbd7-a350-4dc9-ac20-083d10456b6a-image.png
        2. This is the code to open the file
         ifstream reader(":/data/data/Users.txt", ios_base::in);
                if (!reader.is_open())
                {
                    cout << "Not able to open Users.txt" << endl;
                    exit(2);
                }
        

        and the output

        Not able to open Users.txt
        Debugging V1.exe has finished with exit code 2.
        
        1. and I got the data.txt path from here
          b291cc7a-a8b9-494c-9877-7456ec34d3df-image.png

        I'd appreciate it if you could offer me some advice, many thanks.

        J Offline
        J Offline
        JonB
        wrote on 18 May 2022, 08:16 last edited by JonB
        #3

        @muggleR said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

        I'm Currently working on project need to read from a file using c++ fstream

        @ChrisW67 has explained what the issue is with a resource file path and why ifstream is not going to be able to access it. The question is what you are going to do about it:

        • If you do not have a mandatory reason for using a ifstream don't use it! Use the Qt classes instead.
        • If you do have to use it (e.g. you are passing this to some library code which demands a ifstream), I have not tested this but: use QFile(":/data/data/Users.txt"), open it, then there is int QFileDevice::handle() const to access the low-level C file handle. I presume there is a C++ function for generating/wrapping a ifstream from/around a file handle? (UPDATE See https://stackoverflow.com/a/5205191/489865 or https://stackoverflow.com/a/5206221/489865 for this?)
        • Otherwise you won't be able to use Qt resource paths :( If really necessary you can use Qt calls to copy the file in the resource via its path to a temporary, external file, then that can be opened via a ifstream.
        M V 2 Replies Last reply 18 May 2022, 08:49
        5
        • C ChrisW67
          18 May 2022, 07:54

          You are trying to use a Qt embedded resource from a non-Qt function that is not aware of them. The path provided to the ifstream constructor is being interpreted directly against the Windows file system, and no such file exists. The QFile equivalent knows about the Qt resource system and, when it sees the ":" prefix in the path, switches to looking in the compiled-in resources rather than the host file system.

          M Offline
          M Offline
          muggleR
          wrote on 18 May 2022, 08:23 last edited by
          #4

          @ChrisW67 That make sense to me, thanks.

          1 Reply Last reply
          0
          • J JonB
            18 May 2022, 08:16

            @muggleR said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

            I'm Currently working on project need to read from a file using c++ fstream

            @ChrisW67 has explained what the issue is with a resource file path and why ifstream is not going to be able to access it. The question is what you are going to do about it:

            • If you do not have a mandatory reason for using a ifstream don't use it! Use the Qt classes instead.
            • If you do have to use it (e.g. you are passing this to some library code which demands a ifstream), I have not tested this but: use QFile(":/data/data/Users.txt"), open it, then there is int QFileDevice::handle() const to access the low-level C file handle. I presume there is a C++ function for generating/wrapping a ifstream from/around a file handle? (UPDATE See https://stackoverflow.com/a/5205191/489865 or https://stackoverflow.com/a/5206221/489865 for this?)
            • Otherwise you won't be able to use Qt resource paths :( If really necessary you can use Qt calls to copy the file in the resource via its path to a temporary, external file, then that can be opened via a ifstream.
            M Offline
            M Offline
            muggleR
            wrote on 18 May 2022, 08:49 last edited by
            #5

            @JonB Thank you for the reply, I'll consider using QT classes. It seems that using c++ fstream is too treaky in qt system.

            1 Reply Last reply
            0
            • J JonB
              18 May 2022, 08:16

              @muggleR said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

              I'm Currently working on project need to read from a file using c++ fstream

              @ChrisW67 has explained what the issue is with a resource file path and why ifstream is not going to be able to access it. The question is what you are going to do about it:

              • If you do not have a mandatory reason for using a ifstream don't use it! Use the Qt classes instead.
              • If you do have to use it (e.g. you are passing this to some library code which demands a ifstream), I have not tested this but: use QFile(":/data/data/Users.txt"), open it, then there is int QFileDevice::handle() const to access the low-level C file handle. I presume there is a C++ function for generating/wrapping a ifstream from/around a file handle? (UPDATE See https://stackoverflow.com/a/5205191/489865 or https://stackoverflow.com/a/5206221/489865 for this?)
              • Otherwise you won't be able to use Qt resource paths :( If really necessary you can use Qt calls to copy the file in the resource via its path to a temporary, external file, then that can be opened via a ifstream.
              V Offline
              V Offline
              VRonin
              wrote on 18 May 2022, 10:52 last edited by
              #6

              @JonB said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

              I have not tested this but: use QFile(":/data/data/Users.txt"), open it, then there is int QFileDevice::handle() const to access the low-level C file handle.

              This won't work but it's very close to the solution, you can use QTemporaryFile::createNativeFile to expose a file embedded in the resources to then garb its handle and pass it on to fstream if you need to

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              J 1 Reply Last reply 18 May 2022, 11:04
              4
              • V VRonin
                18 May 2022, 10:52

                @JonB said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

                I have not tested this but: use QFile(":/data/data/Users.txt"), open it, then there is int QFileDevice::handle() const to access the low-level C file handle.

                This won't work but it's very close to the solution, you can use QTemporaryFile::createNativeFile to expose a file embedded in the resources to then garb its handle and pass it on to fstream if you need to

                J Offline
                J Offline
                JonB
                wrote on 18 May 2022, 11:04 last edited by
                #7

                @VRonin said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

                This won't work but it's very close to the solution, you can use QTemporaryFile::createNativeFile to expose a file embedded in the resources

                Is this because QFile(":/...") does not use a genuine file handle in QFile::handle()? I sort of thought it would open a genuine OS file handle into the executable, but does it actually implement it (like reads) via purely in-memory access into the resource area, or what? (You know me: I like explanations, not voodoo! :) )

                C 1 Reply Last reply 27 Jun 2022, 00:44
                0
                • J JonB
                  18 May 2022, 11:04

                  @VRonin said in QT muggle not able to open file with c++ fstream. Please give me a hand.:

                  This won't work but it's very close to the solution, you can use QTemporaryFile::createNativeFile to expose a file embedded in the resources

                  Is this because QFile(":/...") does not use a genuine file handle in QFile::handle()? I sort of thought it would open a genuine OS file handle into the executable, but does it actually implement it (like reads) via purely in-memory access into the resource area, or what? (You know me: I like explanations, not voodoo! :) )

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on 27 Jun 2022, 00:44 last edited by
                  #8

                  @JonB Not entirely across the details but a direct memory mapping that could be exposed as-is to non-Qt file processing may be difficult.

                  Qt rcc considers each resource file individually and will compress any file (variable algorithm) before embedding where the space gain is sufficient. This works very well with things like embedded bulk text, XML, or SVG. When the Qt access paths are used these are decompressed in a manner transparent to the application.

                  A non-Qt application inspecting the executable content directly will see compressed data. It may be that Qt unpacks into a memory block and then works from that, in which case I guess direct non-Qt access could work.

                  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