C,C++ Program to create a file on desktop and write some text into it

This program is for the Windows OS only. The location of the desktop folder is first obtained by using the Win32 function SHGetKnownFolderPath. Then PathAllocCombine is used to append the name of the file to obtain the path to the file. Finally, some text is written using the iostream library of C++!

Categories | About |     |  

Parveen,

This C/C++ program automatically obtains the location of your desktop. We do not have to type or hard-code the path. So it will work on any windows computer.

The program creates a notepad file of name "file.txt" and writes some text, say, hello into it. The path of the file is obtained by appending the name of the file to the path of the desktop.

Open a compiler such as Visual Studio, or any compiler configured for C and C++ and Windows. Copy and paste the code and compile it. This code should compile without any issues.

The source code in C++

IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version

Compile using any C++ compiler. The code has self-explanatory comments.

// ONLY FOR WINDOWS 
// NEEDS WINDOWS SET UP 
// COMPILE USING Visual Studio Express, 
// Visual C++ Express or any edition 
// any version 
#include "shlobj_core.h"

#include "pathcch.h"

#pragma comment(lib, "Pathcch.lib")

#include <iostream>

#include <fstream>

#define DESKTOP FOLDERID_Desktop

using namespace std;

int main()
{

  // for storing the path to desktop 
  wchar_t* p;

  // chances of failure are low 
  // so error check not done 
  SHGetKnownFolderPath(DESKTOP, 0, 0, &p);

  // display the path to desktop 
  wcout << "Desktop: " << p << endl;

  // next get the path to your txt file 
  wchar_t* f;

  // concatenates the name of your 
  // file.txt to the desktop path 
  // error check has not been used 
  // because chances of failure are low 
  PathAllocCombine(p, L"file.txt", 0, &f);

  // we have got the path to the 
  // notepad file in "f" 
  // so free the memory allocated 
  // for the desktop path "p" 
  // MSDN docs recommend that a 
  // call to SHGetKnownFolderPath 
  // should be paired with this call 
  CoTaskMemFree(p);

  p = NULL;

  // block for file write operation 
  {

    // open the file for writing 
    // and create it, if doesn't exist 
    wofstream ofs(f);

    // write any text into the file 
    ofs << "hello";

    wcout << "Saved as: " << f << endl;

    // file gets closed automatically 
  }

  // free the memory allocated for 
  // storing the file path 
  LocalFree(f);

  return 0;

}

Explanation of the Code

SHGetKnownFolderPath
This function accepts a wide char pointer as a parameter, and allocates memory for the path to be obtained. When the function returns, the pointer contains the path to your desktop.
What is wcout?
This is the wide char UNICODE equivalent of cout. We have to use wcout because the function SHGetKnownFolderPath gives us the path in a wide char format. We could have used a conversion function for converting from UNICODE to ASCII but that would have complicated the things.
What is PathAllocCombine?
This function is used to concatenate two paths, and it takes into account the direction of the path separator slashes.
What is wofstream?
This is the wide char UNICODE equivalent of ofstream. We have to use wofstream because the function PathAllocCombine gives us the path in a wide char format. We could have used a conversion function for converting from UNICODE to ASCII but that would have complicated the things.
CoTaskMemFree and LocalFree
These functions are the recommended functions for releasing the memory allocated respectively by SHGetKnownFolderPath and PathAllocCombine.

This Blog Post/Article "C,C++ Program to create a file on desktop and write some text into it" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.