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. [SOLVED] QFile open error
QtWS25 Last Chance

[SOLVED] QFile open error

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 26.5k Views
  • 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.
  • N Offline
    N Offline
    nilotpal
    wrote on last edited by
    #1

    Hi All,

    I am trying to read a simple text file through QFile and its returning "Unknown Error".

    @QFile file("E:/123.txt");
    if(file.exists()){
    qDebug()<<"File Exists";

    QFile f(QString("E:/123.txt"));
    if(f.isOpen()){
    f.close();
    }else{
    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
    qDebug()<<f.errorString();
    else
    qDebug()<<"It worked.";
    }
    }@

    On executing I get the following debug lines:
    @File Exists
    Unknown Error@

    What am I missing here?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Is there a specific reason you're using two different QFile objects?

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nilotpal
        wrote on last edited by
        #3

        Not really, that was mistake :(

        Updated my code to:

        @QFile file("E:/123.txt");
        if(file.exists()){
        qDebug()<<"File Exists";

        if(file.isOpen()){
        file.close();
        }else{
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        qDebug()<<file.errorString();
        else
        qDebug()<<"It worked";
        }
        }@

        still having the same error.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          Do you have read permission for the file on the file system? Does it work if you try to access a different file on a different drive?

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nilotpal
            wrote on last edited by
            #5

            Yes i have read permission for the file. In fact I have tried this on a number of files but it never worked.

            Tried this code both on windows and linux but encountered the same issue.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              Oh... you're checking the return value of file.open() incorrectly. Ir returns true on success. Your errorString() and "It worked" cases are backwards.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              1
              • N Offline
                N Offline
                nilotpal
                wrote on last edited by
                #7

                Bull's eye... Thank you for pointing that out

                The code should have been like this:

                @if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
                qDebug()<<file.errorString();
                else
                qDebug()<<"It worked";@

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Sometimes it's the little things that bite you! Glad to help!

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0

                  • Login

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