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. [Resolved]Unable to read my txt file.
Forum Updated to NodeBB v4.3 + New Features

[Resolved]Unable to read my txt file.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 10.3k Views 1 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.
  • L Offline
    L Offline
    lolo2003
    wrote on 28 Oct 2011, 17:28 last edited by
    #1

    Hi,

    I try to code a simple application that reads a txt file. The problem is that when I run it from QT Creator, it doesn't find the txt file. However, when I run the application directly from its directory, the file is correctly opened. So I suppose that the code is right so I don't understand where is the problem. Thanks for helping me.

    the code is :

    @#include <QApplication>
    #include <QFile>
    #include <QString>
    #include <QTextEdit>

    int main(int argc, char **argv)
    {
    QApplication a(argc, argv);
    QTextEdit zoneTexte;
    zoneTexte.setGeometry(100,100,400,200);
    zoneTexte.setReadOnly(true);

    QString texte;
    QFile fichier("test.txt");
    
    if(fichier.open(QIODevice::ReadOnly | QIODevice::Text))
    {
         texte = fichier.readAll();
         fichier.close();
    }
    else texte = "Impossible d'ouvrir le fichier !";
    
    zoneTexte.setText(texte);
    zoneTexte.show();
    return a.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 28 Oct 2011, 17:39 last edited by
      #2

      Welcome to this forum.

      You probably have to set the working directory in qt ctreator. Under "projects" you can find the "Run settings", where you can set the working directory. In your case it is the directory with your file. An alternative would be to include the complete absolute path of your file. E.g. for windows
      @
      QFile fichier("C:/data/test.txt");
      @

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RazrFalcon
        wrote on 28 Oct 2011, 17:45 last edited by
        #3

        So you have a app.exe and test.txt in the same dir, right?

        You can try this code to see current application dir:
        @qDebug()<<QApplication::applicationDirPath();@
        and compare it with your actual application directory.

        1 Reply Last reply
        1
        • T Offline
          T Offline
          tobias.hunger
          wrote on 28 Oct 2011, 17:46 last edited by
          #4

          Have you tried to set the working directory in Projects->Run Settings?

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lolo2003
            wrote on 28 Oct 2011, 18:00 last edited by
            #5

            Thank you,

            I try the both alternatives but it doesn't work so I choose "working directory" by default.
            And by running the qDebug instruction, I get the correct path.

            The solution by including the full path will certainely work...but it will constrain the application.

            I've tried to find how QT Creator run the .exe application and if there was any options regarding the way it is run but no result.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lolo2003
              wrote on 28 Oct 2011, 18:42 last edited by
              #6

              I put my test.txt file in the application ressources.
              Thank for the help.

              1 Reply Last reply
              0

              3/6

              28 Oct 2011, 17:45

              • Login

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