1

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Tim
  • 11
  • 1

3 Answers3

3

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
0

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;
}
AntiClimacus
  • 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
  • Windows does not have /home/user/Desktop. – László Papp 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
  • Where is `OS_WINDOWS` defined? – Brandin Apr 27 '14 at 09:22
  • @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
0

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.

imbtfab
  • 493
  • 4
  • 10