Is there a default code in c++ to write a file(.txt) to a desktop, which could be used for any computer without knowing the leading /desktop?
-
What do you mean by "the leading /desktop"? – Brandin Apr 27 '14 at 09:08
3 Answers
The most portable way is to use Qt, namely QStandardPaths.
The standard library does not have any off-hand support for it, so you will either need to reinvent the wheel or find a robust solution that already exists. Qt is such a thing.
QStandardPaths::DesktopLocation 0 Returns the user's desktop directory.
In which case, you could use QFile
as well as ofstream to write the file to that folder. You would only need to depend on QtCore
for this.
The code would look like this:
#include <QFile>
#include <QStandardPaths>
#include <QDebug>
#include <QTextStream>
...
QFile file(QStandardPaths::locate(QStandardPaths::DesktopLocation, ""));
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
qDebug() << "Failed to open";
QTextStream out(&file);
// Done, yay!
This will gently work across distributions and operating systems that QtCore supports, including, but limited to:
Windows
Linux
Mac
QNX
and so forth.

- 51,870
- 39
- 111
- 135
Just use standard header fstream
with getenv
:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
using namespace std;
int main (int argc, char **argv)
{
if(argc != 2)
{
cerr << "usage: " << argv[0] << " filename" << endl;
return 1;
}
std::ostringstream oss;
#ifdef _WIN32
oss << getenv("HOMEDRIVE") << getenev("HOMEPATH");
#else
oss << getenv("HOME");
#endif
oss << "/" << argv[1];
ofstream f;
f.open (oss.str().c_str());
f << "bar";
f.close();
return 0;
}

- 1,380
- 7
- 22
-
I think he wants the file to be located on the Desktop no matter what OS. So instead of just C:\Users\user\Desktop he also wants it to appear in /home/user/Desktop/ – David Apr 27 '14 at 09:08
-
@David Is there any particular reason to think that /home/user/Desktop/ actually exists in all operating systems? I think thats what Gnome calls it by default, but you can probably change this if desired. – Brandin Apr 27 '14 at 09:15
-
-
@AntiClimacus For Windows, you should use a windows api function - see SHGetFolderPath or SHGetKnownFolderPath – Brandin Apr 27 '14 at 09:16
-
Please use an existing cross-platform solution instead of reinventing it. – László Papp Apr 27 '14 at 09:19
-
-
@Brandin: you're right, refers to this page http://sourceforge.net/p/predef/wiki/OperatingSystems/, there is no OS_WINDOWS ; _WIN32 is the good way for 32 and 64 bits system – AntiClimacus Apr 27 '14 at 09:27
-
@AntiClimacus It looks a little better but if the environment variables are not defined you will end up trying to write to the root of the filesystem (not nice). If you actually use this solution you should at minimum do some sanity checks on those environment variables – Brandin Apr 27 '14 at 09:34
-
The OP never mentioned wanting a cross-platform solution, and until he says so, I think the present of the "visual studio" tag (and lack of other toolchain tags) suggests that it just needs to work on any *Windows* computer. – nobody May 17 '14 at 03:09
Use SHGetKnownFolderPath
with FOLDERID_Desktop (Vista and later), alternatively SHGetFolderPath
with CSIDL_DESKTOP
to obtain the folder that represents the desktop for the current user. Depends on your Windows version targets, there's several functions, and some of them deprecated.

- 493
- 4
- 10