0

I need to read strings from foo.txt:

#include <QDebug>
#include <QFile>
#include <QStringList>
#include <QTextStream>

int main( int argc, char** argv )
{
    QFile file("foo.txt");
    if (!file.open(QIODevice::ReadOnly))
        return 1;
    QStringList stringList;
    QTextStream textStream(&file);
    textStream.readLine();
    while (!textStream.atEnd())
        stringList << textStream.readLine();

    file.close();

    qDebug() << stringList;

    return 0;
}

outPut

file opened, but textStream always empty.

2
  • It works for me on Linux running it at the command line. It prints out ("Good day") which is what I would expect as you don't put the first line into the string list. Commented Oct 2, 2013 at 19:43
  • Problem is: foo.txt in project folder, but .exe in build folder. Can I include foo.txt in .exe for do not worry about" Where is that file?" Commented Oct 2, 2013 at 19:52

1 Answer 1

1

From your comment it would appear that the executable is simply not finding the file as they are in different locations. There are a variety of ways to solve this and it depends on what the final use case is meant to be. Here are some ways you can solve the issue:

  • hard code in the path to the file (relative or absolute)
  • move the file to be in the same directory as the executable
  • use a command line option to tell the executable where the file is
  • use an environment variable to tell the executable where to look

For testing either of the first two options are quick and easy but you will probably want something better than that if you intend to take things further.

Sign up to request clarification or add additional context in comments.

1 Comment

@StasShakirov: it is not specific to Android. What Troubadour is trying to write is that you need to evaluate these options and pick the best for your scenario. You have not shared enough information to help with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.