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. should I open QFile before size()
Forum Updated to NodeBB v4.3 + New Features

should I open QFile before size()

Scheduled Pinned Locked Moved Solved General and Desktop
qfile
4 Posts 3 Posters 474 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.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    I want to call

    QFile file("1.txt");
    auto mysize= file.size();
    

    In fact this code works in my OS. But I am afraid of UB... Should I open file before ask size()?

    JonBJ 1 Reply Last reply
    0
    • D Offline
      D Offline
      DungeonLords
      wrote on last edited by DungeonLords
      #4

      Solution:
      "The documentation states that QFileDevice::size() returns the size of the file. If the function were to require an open file, its documentation would say that. By convention, Qt doesn't document non-requirements."

      1 Reply Last reply
      0
      • D DungeonLords

        I want to call

        QFile file("1.txt");
        auto mysize= file.size();
        

        In fact this code works in my OS. But I am afraid of UB... Should I open file before ask size()?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @DungeonLords
        If it works without opening the file then I imagine it uses a call which has nothing to do with whether the file is open. I agree the docs are not clear on this. If you feel "unsure" about this behaviour, change to using qint64 QFileInfo::size() const, QFileInfo for sure just looks at OS file information without opening it.

        Christian EhrlicherC 1 Reply Last reply
        1
        • JonBJ JonB

          @DungeonLords
          If it works without opening the file then I imagine it uses a call which has nothing to do with whether the file is open. I agree the docs are not clear on this. If you feel "unsure" about this behaviour, change to using qint64 QFileInfo::size() const, QFileInfo for sure just looks at OS file information without opening it.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @JonB said in should I open QFile before size():

          I agree the docs are not clear on this

          All QFile (not QIODevice) functions are working without open() first. Don't think that this should really be documented.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • D Offline
            D Offline
            DungeonLords
            wrote on last edited by DungeonLords
            #4

            Solution:
            "The documentation states that QFileDevice::size() returns the size of the file. If the function were to require an open file, its documentation would say that. By convention, Qt doesn't document non-requirements."

            1 Reply Last reply
            0
            • D DungeonLords has marked this topic as solved on

            • Login

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