The current working directory is the folder that you're executing in. If your program is on the desktop, but you execute from System32, the current working directory is System32 not the desktop.
If you want the current working directory:
Here is what I would recommend, using C++17 and std::filesystem, using it's current_path() function
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <filesystem>
int main()
{
std::filesystem::path cwd = std::filesystem::current_path();
std::filesystem::create_directory("Examples");
cwd /= "Examples";
std::filesystem::path filePath = cwd / "example.txt";
std::ofstream oFile(filePath);
oFile << "Manchester United\n";
oFile.close();
std::cout << "output file path: " << filePath;
std::getchar();
return 0;
}
If you need the path of the executable, use GetModuleFileNameA() in combination with the same std::filesystem goodies:
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <filesystem>
int main()
{
char szExePath[MAX_PATH];
GetModuleFileNameA(GetModuleHandle(NULL), szExePath, MAX_PATH);
std::filesystem::path exePath(szExePath);
exePath.remove_filename();
std::filesystem::create_directory("Examples");
exePath /= "Examples";
std::filesystem::path filePath = exePath / "example.txt";
std::ofstream oFile(filePath);
oFile << "Manchester United\n";
oFile.close();
std::cout << "output file path: " << filePath;
std::getchar();
return 0;
}
and if you're not on Windows @TedLyngmo shared this in the comments which should work almost always:
auto dir = std::filesystem::path(argv[0]).parent_path() / "Examples";