1

How can I get the absolute path to the desktop for the user that is starting my program?

int main () {
  ofstream myfile;
  myfile.open ("C:\\Users\\username\\Desktop\\example.txt");
  myfile << "Writing this to a file" << endl;
  myfile.close();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
StefanTflch
  • 145
  • 2
  • 14
  • Going to be OS specific. If you are on windows you can [do something like this](https://stackoverflow.com/questions/9542611/how-to-get-the-current-users-home-directory-in-windows) – Cory Kramer Aug 20 '16 at 14:37
  • @CoryKramer What do you think "for every computer starting program" means? – nbro Aug 20 '16 at 14:40
  • every user on computer – StefanTflch Aug 20 '16 at 14:41
  • @RedIcon It's important if you specify if your solution must work just on Windows or also on other OS, since it's not clear, in my opinion, even though you added just the tag of windows... – nbro Aug 20 '16 at 14:43
  • only windows i said absolute path to desktop – StefanTflch Aug 20 '16 at 14:44
  • 1
    You probably want [SHGetKnownFolderPath](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188.aspx). – molbdnilo Aug 20 '16 at 15:23
  • it'll be easier to write the file as `"C:/Users/username/Desktop/example.txt"` – phuclv Aug 20 '16 at 17:06
  • @LưuVĩnhPhúc the path to the user's desktop folder is dynamic, you have to query the OS for it. – Remy Lebeau Aug 20 '16 at 17:08
  • @RemyLebeau I mean writing the path separator as `/` instead of `\\`. The "path" is just an example copied from OP's text – phuclv Aug 20 '16 at 17:12
  • @LưuVĩnhPhúc the best option is to not deal with path delimiters at all. The API has functions for concatenating path segments together, let it handle the delimiters for you. – Remy Lebeau Aug 20 '16 at 17:15

1 Answers1

1

Edited : as Remy Lebeau suggested

I want to get absolute path to desktop for every computer starting program?

If you are in windows you need to use the API SHGetFolderPath function, click here for more informations.

When you get the path of the desktop you will need to combine (append) it with your file name, the generated path will represents the full path of the file wich is situated in the desktop, there is the full code:

#include <iostream>
#include <Windows.h>
#include <fstream>
#include <shlobj.h> // Needed to use the SHGetFolderPath function.

using namespace std;

bool GetDesktopfilePath(PTCHAR filePath, PTCHAR fileName)
{
    // Get the full path of the desktop :
    if (FAILED(SHGetFolderPath(NULL,
        CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE,
        NULL,
        SHGFP_TYPE_CURRENT,
        filePath))) // Store the path of the desktop in filePath.
        return false;

    SIZE_T dsktPathSize = lstrlen(filePath); // Get the size of the desktope path.
    SIZE_T fileNameSize = lstrlen(fileName); // Get the size of the file name.

    // Appending the fileName to the filePath :
    memcpy((filePath + dsktPathSize), fileName, (++fileNameSize * sizeof(WCHAR)));

    return true;
}

int main()
{
    ofstream myFile; 

    TCHAR    filePath[MAX_PATH];             // To store the path of the file.
    TCHAR    fileName[] = L"\\Textfile.txt"; // The file name must begin with "\\".

    GetDesktopfilePath(filePath, fileName);  // Get the full path of the file situated in the desktop.

    myFile.open(filePath);                  // Opening the file from the generated path.
    myFile << "Writing this to a file" << endl;
    myFile.close();

    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Why are you manually scanning and copying buffers? Since you are using the Shell API anyway, you should use [`PathAppend()`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773565.aspx) or [`PathCombine()`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb77357..aspx) instead. And I would suggest making `GetDesktopfilePath()` return a `std::string` instead of fill a `char[]` buffer. – Remy Lebeau Aug 20 '16 at 17:03
  • i'm avoiding API for many reasons, especially when it comes to manipulating bytes, anyways, the code worked for you ? – Amrane Abdelkader Aug 20 '16 at 17:06
  • You are using the Shell API anyway, there is no good reason to avoid using other Shell API functions, especially in the same function. Otherwise, at least use `lstrlen()`, `lstrcat()` etc instead of doing things manually. – Remy Lebeau Aug 20 '16 at 17:09
  • i made some changes in the code as you suggested, but the return value of the function is a bool rather than a string. – Amrane Abdelkader Aug 20 '16 at 17:52