0

I am trying to make a file with the C++ code below.

   ofstream myfile;
   myfile.open("example.txt");
   myfile << "Manchester United\n";
   myfile.close();

but I want to save it to a folder labeled "Examples" inside of my project folder. Currently by default, it creates an example.txt file in my project folder itself.

So for this problem, would I need to change the working directory to the Examples folder, write data to the file, and then revert back to the project directory after I'm finished? Or would I need to do something different?

Adam Will
  • 1
  • 3
  • 1
    `myfile.open("Examples/example.txt");` – Igor Tandetnik Jul 05 '20 at 15:29
  • 4
    Use `std::filesystem::current_path` to get or set the current working directory. This needs a compiler that supports C++17. – john Jul 05 '20 at 15:33
  • 1
    If C++17 isn't available to you, here is a whole raft of different approaches for different operating systems: [Best way to get application folder path](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – user4581301 Jul 05 '20 at 15:36
  • The Working Directory moves depending on how you run the program, so if you want files in a particular spot every time, you can't count on the Working Directory alone. – user4581301 Jul 05 '20 at 15:38
  • If you have c++17, `auto dir = std::filesystem::path(argv[0]).parent_path() / "Examples";` could be one way to get the path to a subdirectory relative to where your program is. `dir / "example.txt"` will then give you the full path to the file. – Ted Lyngmo Jul 05 '20 at 15:53
  • 1
    I used `````` instead of `````` and everything works how I wanted thanks for the help! – Adam Will Jul 06 '20 at 00:29

1 Answers1

1

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";
GuidedHacking
  • 2,683
  • 1
  • 7
  • 38