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. The path reference when trying to open a file?
Forum Updated to NodeBB v4.3 + New Features

The path reference when trying to open a file?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 4.4k Views 3 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.
  • Q Offline
    Q Offline
    qtpi
    wrote on last edited by
    #1

    hey follas. I am confused when I tried to open a file normally I do with fopen:

    fopen("path","r");

    path would be "../" or "./" or whatever it should be. but the question is what is the reference to this "./"? the position of the .pro file or the debug/release file?

    I tested and it turns out it should be the .pro file. But I still want to verify my thoughts.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      ./ means "this directory"
      so inside Creator its the project folder.
      If you run your exe from some place else it will be something else.

      ../ mean the parent/one back directory from this one

      For production code, its easy to use

      qDebug() << "App path : " << qApp->applicationDirPath();

      which means where .exe is.

      Q 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        ./ means "this directory"
        so inside Creator its the project folder.
        If you run your exe from some place else it will be something else.

        ../ mean the parent/one back directory from this one

        For production code, its easy to use

        qDebug() << "App path : " << qApp->applicationDirPath();

        which means where .exe is.

        Q Offline
        Q Offline
        qtpi
        wrote on last edited by
        #3

        @mrjj said:

        will

        hi, I know what ./ and ../ means. but I dont know if I wrote ./ in qt, what should be the reference of this "./" ? should this be the path of the executable file or the .pro file?

        I dont know if I made myself clear enough.

        mrjjM 1 Reply Last reply
        1
        • Q qtpi

          @mrjj said:

          will

          hi, I know what ./ and ../ means. but I dont know if I wrote ./ in qt, what should be the reference of this "./" ? should this be the path of the executable file or the .pro file?

          I dont know if I made myself clear enough.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @qtpi
          hi
          ok.
          the value of "./" depends on how its activate and what the current folder is.

          Often it will be from the exe file and most OSes will make current folder the folder of the exe. Via creator it seems that current folder is where project is and not exe.

          if you need to load files from exe
          qApp->applicationDirPath();
          or
          http://doc.qt.io/qt-5/qstandardpaths.html

          should be used instead of "./" and "../"

          ps. and sorry if i still dont get what you are asking :)

          Q 1 Reply Last reply
          3
          • mrjjM mrjj

            @qtpi
            hi
            ok.
            the value of "./" depends on how its activate and what the current folder is.

            Often it will be from the exe file and most OSes will make current folder the folder of the exe. Via creator it seems that current folder is where project is and not exe.

            if you need to load files from exe
            qApp->applicationDirPath();
            or
            http://doc.qt.io/qt-5/qstandardpaths.html

            should be used instead of "./" and "../"

            ps. and sorry if i still dont get what you are asking :)

            Q Offline
            Q Offline
            qtpi
            wrote on last edited by
            #5

            @mrjj said:

            Via creator it seems that current folder is where project is and not exe.

            Via creator it seems that current folder is where project is and not exe.

            this is what I meant. Thank you.

            1 Reply Last reply
            1
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It's worth to point out that your program should never rely on such relative paths, The working directory (./) will change depending on how you start your app.

              If you start it from Qt Creator it will be (by default) the build directory (not .pro location nor executable location). If you go to Project pane and select the "run" page you can change working directory to whatever you want.

              When you run the app by double-clicking the executable the working directory will be the location of the executable.

              When you create a shortcut for it you can change the working directory in its properties to whatever you want.

              When you run a program from a command line the working directory will be the directory you are currently in, which may have nothing to do with the executable location.

              In summary - do as @mrjj said and use standard paths instead of relative ones.

              Q 1 Reply Last reply
              4
              • Chris KawaC Chris Kawa

                It's worth to point out that your program should never rely on such relative paths, The working directory (./) will change depending on how you start your app.

                If you start it from Qt Creator it will be (by default) the build directory (not .pro location nor executable location). If you go to Project pane and select the "run" page you can change working directory to whatever you want.

                When you run the app by double-clicking the executable the working directory will be the location of the executable.

                When you create a shortcut for it you can change the working directory in its properties to whatever you want.

                When you run a program from a command line the working directory will be the directory you are currently in, which may have nothing to do with the executable location.

                In summary - do as @mrjj said and use standard paths instead of relative ones.

                Q Offline
                Q Offline
                qtpi
                wrote on last edited by
                #7

                @Chris-Kawa said:

                If you start it from Qt Creator it will be (by default) the build directory (not .pro location nor executable location). If you go to Project pane and select the "run" page you can change working directory to whatever you want.

                Thanks! That's very informative! The reason I prefer relative path is that if the software is copied on another machine I do not need to notice which directory I have to leave the software in.

                But indeed absolute path is way safer.

                kshegunovK 1 Reply Last reply
                0
                • Q qtpi

                  @Chris-Kawa said:

                  If you start it from Qt Creator it will be (by default) the build directory (not .pro location nor executable location). If you go to Project pane and select the "run" page you can change working directory to whatever you want.

                  Thanks! That's very informative! The reason I prefer relative path is that if the software is copied on another machine I do not need to notice which directory I have to leave the software in.

                  But indeed absolute path is way safer.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @qtpi

                  The reason I prefer relative path is that if the software is copied on another machine I do not need to notice which directory I have to leave the software in.

                  But indeed absolute path is way safer.

                  It's not a matter of using or not using relative paths, it's a matter of their "relativity" - what are they relative to. If you write ../app this depends on the current working directory which may change, but /home/myuser/test/../app is another matter - you have the ../app fragment now relative to /home/myuser/test and this isn't changing. So ultimately you'd get the same behavior every time.

                  The point is you should retrieve the application executable's path and then base your paths on that or use one of the standard locations (e.g. QStandardPaths::AppDataLocation), just as @mrjj wrote. A starting point. ./ just means nothing because the current working directory (the . dir) isn't rigidly defined (see Chris's explanation), it depends on how and who starts the application.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You can also override the working directory at runtime. For example to always use the location of the executable as the "base" you can call something like this somewhere at the start of your app:

                    QDir::setCurrent(qApp->applicationDirPath());
                    

                    It might have some caveats on some platforms though, see the docs.

                    1 Reply Last reply
                    3

                    • Login

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