Consider a line of simple C code:

fopen("C:\\project\\test1.dat","r");
it "directs" the program to the directory where it can find the file from which it will read.

My question is under what circumstances, I may write
fopen("test1.dat","r") instead?

Does the program need to be in the same folder / directory as test1.dat?

Thanks

Dani AI

Generated

A few clarifications and practical tips that build on what and said.

Relative filenames are resolved against the process's current working directory (CWD), and that CWD is not always the folder that contains the executable. How you start the program matters: a shell inherits its CWD, a shortcut can set a different "Start in" folder, many IDEs set the debugger's working directory in project settings, and services or scheduled tasks often use system folders (for example, Windows services commonly default to C:\Windows\System32). For embedded or some tiny RTOS builds there may be no CWD at all, as warned.

Quick diagnostics (C++17+): print the CWD and test existence with std::filesystem before trying to open a file.

#include <filesystem>
#include <iostream>
#include <fstream>

int main() {
    std::cout << "CWD: " << std::filesystem::current_path() << '\n';
    auto p = std::filesystem::current_path() / "config.ini";
    if (!std::filesystem::exists(p)) {
        std::cerr << p << " not found\n";
        return 1;
    }
    std::ifstream in{p};
    // ...
}

If you want a stable place for resources, compute a path relative to the executable directory (safer than depending on CWD). Example helpers (platform-specific):

#ifdef _WIN32
// use GetModuleFileNameA to get exe path, then parent_path()
#else
// use readlink("/proc/self/exe", ...) on Linux; fallback to current_path()
#endif

Practical troubleshooting checklist

  • Print the absolute path you try to open to confirm it is what you expect.
  • Check file permissions and user account the process runs under.
  • In IDEs, check debug/run settings for the working directory.
  • Prefer std::filesystem::path for joins and checking existence.
  • For deployed apps, supply absolute paths via config or derive them from the executable directory; avoid assuming CWD in privileged processes for security reasons.

These steps expand on 's and 's points and should help find why a relative name sometimes fails and how to fix it robustly.

Recommended Answers

All 3 Replies

Consider a line of simple C code:

fopen("C:\\project\\test1.dat","r");
it "directs" the program to the directory where it can find the file from which it will read.

My question is under what circumstances, I may write
fopen("test1.dat","r") instead?

Does the program need to be in the same folder / directory as test1.dat?

Thanks

If you use fopen("test1.dat","r") which is the typical usage (no path information given), fopen will look in the current working directory. This the directory where your project file (.dsw in case of VC++ 6.0 and .sln in case of VC++ 7.1) is present. If u run your programe from explorer, say by clicking on it, current woring directry is one in which .exe of your application is present.

In addition, if the file is NOT one the current working directory the program may have to change the current working directory to where the file is. There are a few operating systems where there is no concept of "current working directory", in which case the program has no choice but to specify the full path.

thanks dragon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.