My Documents folder location

I want to get the path of My Documents Folder in the vaiable MY_DOCUMENTS

char MY_DOCUMENTS[60];

Any help?
1) This is Windows-specific, so really belongs in the Windows Programming forum.

2) Have a look at http://lmgtfy.com/?q=How+to+get+path+to+My+Documents
I've already tried that. But I was unsuccessful.
Can anyone please provide a code fragment in which My document location store in "MY_DOCUMENTS"

Please help
I've already tried that. But I was unsuccessful.

You're joking, right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <Windows.h>
#include <ShellAPI.h>
#include <KnownFolders.h>
#include <ShlObj.h>

int main()
{
	CoInitialize(NULL);

	TCHAR* path = 0;
	SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_SIMPLE_IDLIST, NULL, &path);
	CoTaskMemFree(path);

	return 0;
}
Last edited on
I want to save file using file handling in My documents folder and make new directories in My documents folder

But in every one's PC this address is different, depends upon his username and in which drive has he installed windows (C,D,E or other)

In above program the address is stored in "path" but how can I use it to make directory(_mkdir()) and save file
Lookup calls:
CreateDirectory
CreateFile (or use ofstream)
1
2
3
4
5
6
char dir[50] = { "C:\\Users\\User\\Documents\\save.dat" };
ofstream A;
A.open(dir);
.
.
.


I know this will work for me
But in everyone's PC this address is different

like you provided the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <Windows.h>
#include <ShellAPI.h>
#include <KnownFolders.h>
#include <ShlObj.h>

int main()
{
	CoInitialize(NULL);

	TCHAR* path = 0;
	SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_SIMPLE_IDLIST, NULL, &path);
	CoTaskMemFree(path);

	return 0;
}



But how to get path to My documents in "dir"
Last edited on
Really? You can't look up how to copy a string from one array to another?
I believe the below might give you another hint.

%USERPROFILE%\Documents

This will direct you to the user's documents
That assumes the documents folder is called "Documents" under the home directory.

It can be mapped elsewhere by sysadmins.
Last edited on
Topic archived. No new replies allowed.