How to create a text file in a folder on the desktop

26,969

Create a file and write some text to it is simple, here is a sample code:

   #include <iostream>
   #include <fstream>
   #include <string>
   using namespace std;

   int main() 
   {
      std::ofstream o("/Users/sample/Desktop/save.txt");

      o << "Hello, World\n" << std::endl;

      return 0;
   }

I hope that answers your question but I am not sure if i understand your question correctly, If not please add the details correctly of what you are trying to acheive.

[Update]: Okay I guess the comment clears the problem.
Your real question is, You want to save the file in the desktop of the user who is playing the game. So getting the path of the current user's desktop is the problem.

I am not sure if there is an portable way to get desktop path but it can be done in following ways:

In Windows:
Using the SHGetSpecialFolderPath() function.

Sample code:

char saveLocation[MAX_PATH] = {0};

SHGetSpecialFolderPath(NULL, saveLocation, CSIDL_DESKTOPDIRECTORY, FALSE);

//Now saveLocation contains the path to the desktop
//Append your file name to it
strcat(saveLocation,"\\save.txt");

ofstream o(saveLocation);

In Linux:
By using environment variables $HOME

sample code:

string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);
Share:
26,969

Related videos on Youtube

muhammedkasva
Author by

muhammedkasva

Updated on November 28, 2020

Comments

  • muhammedkasva
    muhammedkasva over 3 years

    I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code:

    ofstream example("/Users/sample/Desktop/save.txt");

    But I want to it could been run the other mac. I don't know what I should write addres for save.txt.

    Can anyone help me?

    • Matteo Italia
      Matteo Italia almost 13 years
      So, you want the "right way" to obtain the path to the desktop, correct?
  • Xeo
    Xeo almost 13 years
    Based on "but ı want to it could been run the other mac.ı don't know what ı should write addres for save.txt.can anyone help me?" I think OP knows how to write to a text file and wants a portable way to obtain the desktop path.
  • Kerrek SB
    Kerrek SB almost 13 years
    @Xeo: What's a "portable desktop"? :-)
  • Xeo
    Xeo almost 13 years
    @Kerrek: Where do I say anything about a portable desktop? - "a portable way to obtain the desktop path"
  • Kerrek SB
    Kerrek SB almost 13 years
    @Xeo: sorry, it was a joke... the entire idea of obtaining the path presumes that there is a path to be found in the first place, so it sort of presumes the existence of a desktop, which I dubbed the "portable desktop". Never mind though!
  • muhammedkasva
    muhammedkasva almost 13 years
    well the problem is that it will compile only in comp with samp user , but i want to inside my project so that it couldve compiled in any comp, it's kind of savegame file.. i hope its clearer now
  • Alok Save
    Alok Save almost 13 years
    @muhammedkasva: I updated the answer, Does that solve the problem?
  • muhammedkasva
    muhammedkasva almost 13 years
    ı wrote your updated answer but it gives error "use of undeclerad identifier [MAX_PATH] " how can ı solve this error.Also which library can ı use??
  • Alok Save
    Alok Save almost 13 years
    @muhammedkasva: MAX_PATH is a macro which defines maximum range of the path, You can keep it something like #define MAX_PATH 256
  • muhammedkasva
    muhammedkasva almost 13 years
    well ı did it is fixed.at now the same error for "CSIDL_DESKTOPDIRECTORY"
  • Alok Save
    Alok Save almost 13 years
    Did you check the msdn link i added to SHGetSpecialFolderPath() function? You need to include Shlobj.h and link to library Shell32.lib, please read that link.
  • muhammedkasva
    muhammedkasva almost 13 years
    ı use xcode 4. ı tried to d but it gives error.can you tell me step b step
  • Alok Save
    Alok Save almost 13 years
    @muhammedkasva: How can you use Windows functions on Mac book?
  • muhammedkasva
    muhammedkasva almost 13 years
    ı said that macbook in the beginning.ı thought you tell this code according to macosx
  • Alok Save
    Alok Save almost 13 years
    @muhammedkasva: Mac & Windows are two different OS you cannot use functions from Windows library on Mac simply because Mac won't use those libraries. Linux code works on Mac because the underneath Mac OS uses Linux. You should use the Linux code sample I gave in the answer.