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. How do I get username and appname for file path
Qt 6.11 is out! See what's new in the release blog

How do I get username and appname for file path

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 4.1k 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.
  • L Offline
    L Offline
    LovelyGrace
    wrote on last edited by
    #1

    How am I able to define a path like "C:/Users/<USER>/AppData/Local/<APPNAME>", for different username and app? How do I set this to automatically get the user and the appname? Thank you.

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

      Hi
      You can use https://doc.qt.io/qt-5.9/qstandardpaths.html
      and QStandardPaths::AppDataLocation

      1 Reply Last reply
      3
      • L Offline
        L Offline
        LovelyGrace
        wrote on last edited by
        #3

        how about in c++, not using qstandardpath?

        jsulmJ 1 Reply Last reply
        0
        • L LovelyGrace

          how about in c++, not using qstandardpath?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LovelyGrace said in How do I get username and appname for file path:

          qstandardpath

          Why don't you want to use it?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LovelyGrace
            wrote on last edited by
            #5

            im not sure if its gonna work with fsteam. my program looks like this:

            fsteam file;
            file.open(path);

            mrjjM 1 Reply Last reply
            0
            • L LovelyGrace

              im not sure if its gonna work with fsteam. my program looks like this:

              fsteam file;
              file.open(path);

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

              @LovelyGrace
              hi
              Well you can always use
              https://doc.qt.io/qt-5/qdir.html#toNativeSeparators

              1 Reply Last reply
              3
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                and https://doc.qt.io/qt-5/qstring.html#toStdString

                if fsteamdoes not accept QStrings


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2
                • L Offline
                  L Offline
                  LovelyGrace
                  wrote on last edited by
                  #8

                  I also assume that, do u have anysuggestion using fstream?

                  J.HilkJ mrjjM 2 Replies Last reply
                  0
                  • L LovelyGrace

                    I also assume that, do u have anysuggestion using fstream?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #9

                    @LovelyGrace
                    yes use QFile.

                    Sry never heard of fstream... my bad, I read fsteam instead.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • L LovelyGrace

                      I also assume that, do u have anysuggestion using fstream?

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

                      @LovelyGrace said in How do I get username and appname for file path:

                      do u have any suggestion using fstream?

                      Hi.
                      What you mean ?
                      How it works or ?
                      http://www.cplusplus.com/doc/tutorial/files/

                      1 Reply Last reply
                      1
                      • L Offline
                        L Offline
                        LovelyGrace
                        wrote on last edited by
                        #11

                        I still like to use fstream but I don't know how am I able to set the path to C:/Users/<USER>/AppData/Local/<APPNAME>", as user and appname is unknown.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          LovelyGrace
                          wrote on last edited by
                          #12

                          what I did is like this:

                          QString appdata = getenv("appdata");
                          // appdata += "\DoDLog.log";
                          // fstream stud;
                          // stud.open(appdata.toStdString().c_str(), ios::app);

                          but it goes to appdata/roaming/DoDLog.log
                          how can I set to appdata/local/<myapplication>/DoDLog.log

                          JonBJ Pl45m4P 2 Replies Last reply
                          0
                          • L LovelyGrace

                            what I did is like this:

                            QString appdata = getenv("appdata");
                            // appdata += "\DoDLog.log";
                            // fstream stud;
                            // stud.open(appdata.toStdString().c_str(), ios::app);

                            but it goes to appdata/roaming/DoDLog.log
                            how can I set to appdata/local/<myapplication>/DoDLog.log

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #13

                            @LovelyGrace

                            but it goes to appdata/roaming/DoDLog.log
                            how can I set to appdata/local/<myapplication>/DoDLog.log

                            Did you look through/try the various entries from https://doc.qt.io/qt-5.9/qstandardpaths.html#StandardLocation-enum ? (I have no idea why you want to use getenv() or fstream or whatever.) E.g. from the example paths returned shown in the table, perhaps:

                            AppConfigLocation	"C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"
                            
                            1 Reply Last reply
                            4
                            • L LovelyGrace

                              what I did is like this:

                              QString appdata = getenv("appdata");
                              // appdata += "\DoDLog.log";
                              // fstream stud;
                              // stud.open(appdata.toStdString().c_str(), ios::app);

                              but it goes to appdata/roaming/DoDLog.log
                              how can I set to appdata/local/<myapplication>/DoDLog.log

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by Pl45m4
                              #14

                              @LovelyGrace

                              There's an API for this (If your application is Windows-only). Dont know if it works in your case but I would try this:
                              https://stackoverflow.com/questions/11587426/get-current-username-in-c-on-windows

                              But I guess QStandardPath and friends are better ways...

                              @LovelyGrace said in How do I get username and appname for file path:

                              as user and appname is unknown.

                              Why is the current user name unknown? If your app is running on a machine, there is always an active (logged-in) user. If you are planning to read / write data to other user's homedir, you need to know their names or you use every subfolder in "C:\Users...."

                              Why are you using QString, but dont want to use QFile or QStandardPath?

                              Btw, like @JonB already said, QStandardPath provides PathVariables that will lead to your destination:

                              
                              DataLocation        "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"
                              

                              or

                              AppConfigLocation	"C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"
                              

                              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                              ~E. W. Dijkstra

                              1 Reply Last reply
                              1

                              • Login

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