Using a text file as resource of the project



  • I'm trying to access a text file using a function in c++ to read this but I don't know how access this text file.
    The text file are in the project folder and was added into the project as a resource. I already tryed put the full url using the prefix but still not working.

    the .qrc file

    <RCC>
        <qresource prefix="/NN">
            <file alias="dumped">dumped.nnet</file>
        </qresource>
    </RCC>
    

    P.S. I'm trying this on a Android project.





  • Hi,

    Sample code for fetching the data from resource file
    I have used prefix and added sampleText.txt

    Then using QFile class to read the data from the text file.

    void RessourceFile::getData()
    {

    QString data;
    QString fileName(":/new/prefix1/sampleText.txt");
    
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)) {
        qDebug()<<"filenot opened"<<endl;
    }
    else
    {
        qDebug()<<"file opened"<<endl;
        data = file.readAll();
    }
    
    file.close();
    
    qDebug()<<data<<endl;
    

    }

    Which worked in Desktop and android platforms.
    Hope this helps you.

    Thanks,



  • This post is deleted!


  • I already have a function to read the file and its works but only when I put the file directly into the memory by usb and pass the path to the function. What I need is a way to do this using the file inside the project but when I try pass to the function this :
    ":/NN/dumped"
    the function can't find the file.

    my funcion:

    Works
    keras::KerasModel m("/sdcard/Documents/dumped.nnet");

    Don't work
    keras::KerasModel m(":/NN/dumped" );


  • Qt Champions 2017

    @OliveiraNT
    Hi
    Inside KerasModel where you try to read the file, do you use
    QFile to read it?
    Other file function ( std c++) do not understand ":/" syntax.



  • @mrjj No, I'not using QFile is a std function.
    Is there any way to do this in std or I have to change to QFile?


  • Qt Champions 2017

    @OliveiraNT
    You must use QFile.
    the text file is actually embedded into the exe file and only
    QFile can read it from there.
    Other file classes simply do not know about resources and hence QFile must be used.



  • Ok thanks for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.