0

I have text files in my resource file and I'd like to be able to provide a path for this file to std::ifstream. Neither :\file_name.txt nor ..\file_name.txt works.

Does anyone know how to fix it?

Mat
  • 202,337
  • 40
  • 393
  • 406
user336635
  • 2,081
  • 6
  • 24
  • 30
  • possible duplicate of [Relative path for fstream](http://stackoverflow.com/questions/8068921/relative-path-for-fstream) – Alok Save Dec 11 '11 at 11:10
  • @Als: I don't think so. Qt resources are not filesystem objects, they're bundled inside the executable at link time. – Mat Dec 11 '11 at 11:11
  • That question has nothing to do with this. – Luca Carlon Dec 11 '11 at 11:12
  • @user336635: you are referring to Qt resources, compiled with `qrc`, right? – Mat Dec 11 '11 at 11:13
  • @Als but there wasn't satisfying answer given to yes, similar question. It would be nice if the question could actually get correct answer. – user336635 Dec 11 '11 at 11:13

1 Answers1

2

Qt resource files are not filesystem files. Those files are loaded in memory as static char arrays. You can see for yourself looking in your build directory for qrc_*.cpp files. You can get data from there if you want, or you might want to use QTextStream for reading those, using the QIODevice constructor with a QFile.

You don't specify what you want to do exactly, but this is a sample that reads what is inside the file:

#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QFile>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file(":/test.txt");
    QTextStream stream(&file);
    if (!file.open(QIODevice::ReadOnly)) {
       qFatal("Failed to open file.");
       return -1;
    }
    QString text = stream.readAll();
    if (text.isNull()) {
       qDebug("Failed to read file.");
       return -1;
    }
    qDebug("File content is: %s. Bye bye.", qPrintable(text));
    return 0;
}
Luca Carlon
  • 9,546
  • 13
  • 59
  • 91