Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Opening file from .qrc from C program



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


  • Moderators

    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.


  • Qt Champions 2019

    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.



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



  • @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.


  • Moderators

    @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



  • @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.


  • Moderators

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


Log in to reply