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
Forum Update on Monday, May 27th 2025

[SOLVED] QFile open error

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 26.6k 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 3 Aug 2011, 20:19 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 3 Aug 2011, 20:26 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 3 Aug 2011, 20:31 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 3 Aug 2011, 20:33 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 3 Aug 2011, 20:35 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 3 Aug 2011, 20:41 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 3 Aug 2011, 20:47 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 3 Aug 2011, 20:49 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

                  1/8

                  3 Aug 2011, 20:19

                  • Login

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