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 file from .qrc from C program
Qt 6.11 is out! See what's new in the release blog

Opening file from .qrc from C program

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 3.2k Views 2 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.
  • K Offline
    K Offline
    krokstr
    wrote on last edited by
    #1

    Hello,

    I have ported complex C program inside my C++. So the C program have to read a file and manipulate it , but I don't want the input file to be accessible by the user. That's why I made .qrc file and putted the input file in there, but now the problem is that C read function isn't able to read the input, because it's not part of the filesystem. Is there a way doing this, except making temporary file with QFile and deleting it after that? My other option is reading the file to QByteArray and reimplementing the C read function to accept array as an input, but that's my last option.

    J.HilkJ Pablo J. RoginaP 2 Replies Last reply
    0
    • K krokstr

      Hello,

      I have ported complex C program inside my C++. So the C program have to read a file and manipulate it , but I don't want the input file to be accessible by the user. That's why I made .qrc file and putted the input file in there, but now the problem is that C read function isn't able to read the input, because it's not part of the filesystem. Is there a way doing this, except making temporary file with QFile and deleting it after that? My other option is reading the file to QByteArray and reimplementing the C read function to accept array as an input, but that's my last option.

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

      hi @krokstr , as far as I know, it's not possible to access the qrc-System from "outside"
      and the 2 solutions you mentiond, temp file a qbytearray, are so only way around it.

      But QByteArray provides the data in a char array with size specifications, -> should not be to difficult to port it to your c-code.


      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.

      1 Reply Last reply
      2
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The only way I see is to save it to a temporary file since a non-Qt programm can't access a .qrc - file directly.

        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
        • K Offline
          K Offline
          krokstr
          wrote on last edited by
          #4

          Thanks for the fast and accurate answering! One last question(it may not be the right place to ask), I am making it on android platform, so if I make it with temporary file, is there a safe place to save the file (accessible place only from the APP, "forbidden" from "outside" access even for root user). I've searched for this solution, but didn't found anything which may work for me, so I thought that's the right moment to ask in the forum.

          J.HilkJ 1 Reply Last reply
          0
          • K krokstr

            Hello,

            I have ported complex C program inside my C++. So the C program have to read a file and manipulate it , but I don't want the input file to be accessible by the user. That's why I made .qrc file and putted the input file in there, but now the problem is that C read function isn't able to read the input, because it's not part of the filesystem. Is there a way doing this, except making temporary file with QFile and deleting it after that? My other option is reading the file to QByteArray and reimplementing the C read function to accept array as an input, but that's my last option.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @krokstr said in Opening file from .qrc from C program:

            making temporary file with QFile and deleting it after that?

            I think that's the easiest approach given what you have already (C program ported, function for reading file, etc.). However consider that it might be the case where the whole program hangs/crashes in the middle of the process and the temporary file could be lying around...

            So if you already accept that the "input file" will be always the same (as it's stored in the .qrc) I suggest moving to a "all-in-memory" approach, that is, go by reading the resource file directly into a byte array.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            K 1 Reply Last reply
            0
            • K krokstr

              Thanks for the fast and accurate answering! One last question(it may not be the right place to ask), I am making it on android platform, so if I make it with temporary file, is there a safe place to save the file (accessible place only from the APP, "forbidden" from "outside" access even for root user). I've searched for this solution, but didn't found anything which may work for me, so I thought that's the right moment to ask in the forum.

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

              @krokstr have a look at the QStandardPath class http://doc.qt.io/qt-5/qstandardpaths.html

              I think, QStandardPaths::AppDataLocation is on android part of the sandbox and not accessible from ouside/other programs


              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.

              1 Reply Last reply
              2
              • Pablo J. RoginaP Pablo J. Rogina

                @krokstr said in Opening file from .qrc from C program:

                making temporary file with QFile and deleting it after that?

                I think that's the easiest approach given what you have already (C program ported, function for reading file, etc.). However consider that it might be the case where the whole program hangs/crashes in the middle of the process and the temporary file could be lying around...

                So if you already accept that the "input file" will be always the same (as it's stored in the .qrc) I suggest moving to a "all-in-memory" approach, that is, go by reading the resource file directly into a byte array.

                K Offline
                K Offline
                krokstr
                wrote on last edited by
                #7

                @Pablo-J.-Rogina Yes, the input file will be one and always with the same name. It's going to be used very rare. But I am afraid of that approach with the temp file), because of exposing the information in it(unless there is a safe place to temporary save it).

                @J-Hilk I am using QStandardPaths::AppDataLocation, but it is accessible for root user and want to exclude this option.

                J.HilkJ 1 Reply Last reply
                0
                • K krokstr

                  @Pablo-J.-Rogina Yes, the input file will be one and always with the same name. It's going to be used very rare. But I am afraid of that approach with the temp file), because of exposing the information in it(unless there is a safe place to temporary save it).

                  @J-Hilk I am using QStandardPaths::AppDataLocation, but it is accessible for root user and want to exclude this option.

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

                  @krokstr I may be wrong here, but I don't think theres anywhere you can hide files from a root user on android. At least not from the level where all apps live. Maybe there's a way to access the underlying linux kernel, but I wouldn't know where to start looking.


                  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.

                  1 Reply Last reply
                  1

                  • Login

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