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. I cannot read from text file
Forum Updated to NodeBB v4.3 + New Features

I cannot read from text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 316 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.
  • mangekoyuM Offline
    mangekoyuM Offline
    mangekoyu
    wrote on last edited by
    #1

    I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
    when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?

    QString appDir = QDir::currentPath() + "/test.txt";
     QString version;
     QFile file(appDir);
     if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
       QTextStream in ( & file);
    
        version = in .readLine();
     
       file.close();
    
     }
    
     qDebug() << version;
     qDebug() << appDir;
    }
    
    J.HilkJ JonBJ 2 Replies Last reply
    0
    • mangekoyuM mangekoyu

      I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
      when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?

      QString appDir = QDir::currentPath() + "/test.txt";
       QString version;
       QFile file(appDir);
       if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
         QTextStream in ( & file);
      
          version = in .readLine();
       
         file.close();
      
       }
      
       qDebug() << version;
       qDebug() << appDir;
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @mangekoyu said in I cannot read from text file:

      I need to read text.txt file that located on my application directory

      is it really? or is it at your source tree/project file directory. Those are 2 are different things.
      QDir::currentPath() uses the path of the executable that was compiled and is usually in a shadow build directory parallel to your source tree directory.

      And it may be even more complicated when you're on a Mac, I assume this is Windows?

      print QDir::currentPath() via QDebug and check if its the path you expect


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      mangekoyuM 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @mangekoyu said in I cannot read from text file:

        I need to read text.txt file that located on my application directory

        is it really? or is it at your source tree/project file directory. Those are 2 are different things.
        QDir::currentPath() uses the path of the executable that was compiled and is usually in a shadow build directory parallel to your source tree directory.

        And it may be even more complicated when you're on a Mac, I assume this is Windows?

        print QDir::currentPath() via QDebug and check if its the path you expect

        mangekoyuM Offline
        mangekoyuM Offline
        mangekoyu
        wrote on last edited by mangekoyu
        #3

        @J-Hilk I checked with qDebug()
        and saw exactly the path I wanted.

        I download a txt file from the internet and it automatically saves it to the src folder.

        QDir::currentPath() gives me the path of the src/ folder. My problem here is that the file I'm trying to read is in the src folder?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          So, if you are 100% that the file is there, and that it does indeed have name test.txt, then are you also sure that the first line of that file contains any text? You are only reading the first line so if it's empty, version will be an empty string for sure, too.

          To make sure your file gets opened, change the code to (for example):

          if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
             QTextStream in ( & file);
              version = in .readLine(); 
             file.close();
           } else {
            qDebug() << "ERROR! Could not open file at:" << appDir;
          }
          

          (Z(:^

          1 Reply Last reply
          3
          • mangekoyuM mangekoyu

            I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
            when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?

            QString appDir = QDir::currentPath() + "/test.txt";
             QString version;
             QFile file(appDir);
             if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
               QTextStream in ( & file);
            
                version = in .readLine();
             
               file.close();
            
             }
            
             qDebug() << version;
             qDebug() << appDir;
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mangekoyu
            Your code will print empty string for version without any warning or indication if it cannot open the file. Don't you think your code should report that situation like @sierdzio says?

            QDir::currentPath() gives me the path of the src/ folder.

            If it does that's "by coincidence". Might work from Qt Creator. Won't work if you run from a terminal. It's the application's current directory. Which you don't know (depends how it's run) and could be anything at any time. You cannot reliably locate a source folder you use in development at runtime from an application. And when deployed it may not even be present. In short, don't use it.

            Finally:

            version = in .readLine();

            What are you expecting this to return anyway. It's the first line of a file. How do we know whether the file has a blank/no first line?

            1 Reply Last reply
            1

            • Login

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