Question

Let's say I want to create a file and my project folder is located inside a folder on my Desktop. How would I create a file using ofstream onto my desktop?

Was it helpful?

Solution

It seems your question could be reformulated as "How do I get the Desktop Path in a operating system?".

Once you have it, just join it with your filename and then use ofstream. No secrets!

ofstream file;
file.open( YOUR_FULL_PATH_HERE, ios::binary ); // suppose here you want binary!
// write your stuff here...
file.close();

The way you obtain the Desktop path depends on the operating system. In Windows, for example, you can use SHGetSpecialFolderPath( CSIDL_DESKTOP ), but in a Linux or Mac OS X, you gotta check how it's done (probably getenv( "HOME" ) will be enough for you to get the Desktop from the home directory for these "UNIX-based" cases).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top