Results 1 to 6 of 6

Thread: QFile::exists(name) verifies PATH to name, but not only FILE name existence

  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default QFile::exists(name) verifies PATH to name, but not only FILE name existence

    On my Windows platform, I have noticed that QFile::exists(name) returns true in case "name" is a directory name, but not a file.
    So, it means this method verifies PATH existence, but not a file existence. This is not what is in manual.
    Am I missing something here?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFile::exists(name) verifies PATH to name, but not only FILE name existence

    I see what you are referring to. Not sure if it's a bug in the code or incorrect doc. Either way, use QFileInfo and you can check both the existence and isFile or isDir, etc.

  3. #3
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: QFile::exists(name) verifies PATH to name, but not only FILE name existence

    Yes,
    thanks
    I know that
    But instead of using to calls (isDir() and isFile()), I can use just one QFile.exists() in order to learn if path is does not exist.
    But I am afraid it will not work always, because it is AGAINST the manual, which said QFile.exists() will return false if FILE does not exists. So, the fact that it returns TRUE if DIRECTORY exists it could be a bug or platform dependence or may be bad overload somewhere...
    That is my question: is it expected behavior from QFile.exists() return true on DIRECTORY existence and return false on FILE or DIRECTORY absence - practically verifies path existence instead of file existence.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFile::exists(name) verifies PATH to name, but not only FILE name existence

    It makes sense on some level that passing a directory name (or file name) to QFile::exists() returns true if the item exists. If you wanted to guarantee that the item exists *and* is a file, you'd have to do as I suggested in my first post.

    If you feel that this is a bug, I'd recommend that you submit a bug report.

    Good luck.

  5. #5
    Join Date
    May 2025
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile::exists(name) verifies PATH to name, but not only FILE name existence

    10 years later (from the above) and the documentation still stumps me. It would be nice to know that if I have a file "c" (no extension) in directory "a/b/", path = "a/b/c", will .isDir() return true or false? Will .isFile() or .exists() return true or false. It is not clear from the documentation I have scanned, and AI gives me confusing answers - a confusing mix of "just checks the syntax, not the actual disk presence" etc. Some simple examples in the documentation would be brilliant.
    Anyway, just a note. Will continue looking :-).

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFile::exists(name) verifies PATH to name, but not only FILE name existence

    It isn't clear to me what you are asking here. Are you saying you might have a situation where you have both a file named "c" in the directory "a/b/" and a subdirectory named "c" in the same directory (i.e. "a/b/c/")? Is that even possible in any OS?

    If "c" is a file in "a/b/", then QFileInfo::isDir() will return false, QFileInfo::isFile() will return true, and both QFileInfo::exists() and QFile::exists() will return true.

    If "c" is a subdirectory in "a/b/", then QFileInfo::isDir() will return true, QFileInfo::isFile() will return false, and both QFileInfo::exists() and QFile::exists() will return false.

    If "c" is a symbolic link to either a file or subdirectory, then the same rules apply as if it were an actual file or directory. Note that the behaviour of QFileInfo::isSymLink() and QFileInfo::isSymbolicLink() is different on Windows.

    QFile derives from QFileDevice so it represents only file objects, not directories.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QFile::exists( filename ) slow
    By lni in forum Qt Programming
    Replies: 9
    Last Post: 25th February 2015, 23:19
  2. Replies: 3
    Last Post: 19th September 2014, 07:23
  3. Checking the existence of a symbolic link file
    By ofer88 in forum Qt Programming
    Replies: 7
    Last Post: 15th July 2012, 21:17
  4. Replies: 3
    Last Post: 9th August 2011, 08:37
  5. Replies: 2
    Last Post: 21st February 2011, 14:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.