How do I read a file thats in a folder, but is in the same folder as the exe.? And how do I do this no matter what folder the root folder is placed in?

Dani AI

Generated

Two different things get mixed together in the thread: the process current working directory (what getcwd/_getcwd returns) and the folder that contains the executable. As suggested, inspecting the current working directory is fine when the working directory is guaranteed to be the base for asset lookups. As correctly noted, the executable's folder and the CWD are not the same thing by requirement, so the chosen approach should match the intended behavior.

For modern, portable C++ (C++17+), prefer std::filesystem for building paths instead of manual string concatenation. This avoids separator bugs and handles long paths:

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

std::filesystem::path p = std::filesystem::current_path() / "data" / "file.txt";
std::ifstream in(p);
if (!in) std::cerr << "Failed to open: " << p << '\n';

When the file must live next to the executable (bundle-style assets), query the executable path and build relative to that. On Windows, GetModuleFileName returns the full exe path; on Linux use readlink("/proc/self/exe", ...); on macOS use _NSGetExecutablePath. Example (Windows):

#include <windows.h>
#include <filesystem>

char buf[MAX_PATH];
GetModuleFileNameA(NULL, buf, MAX_PATH);
std::filesystem::path exeDir = std::filesystem::path(buf).remove_filename();
std::filesystem::path file = exeDir / "assets" / "file.txt";
std::ifstream in(file);

Practical tips: prefer while (in.get(ch)) over while (!in.eof()) for reading (better stream-state handling), print the absolute path being opened when debugging, and check the stream state after open. Avoid strcat-style concatenation; use std::filesystem::path::operator/. Remember IDEs often set a different working directory (project root or bin), so confirm run-time CWD when diagnosing missing-file errors.

Recommended Answers

All 6 Replies

first call getcwd() or _getcwd() to get the current path then concantinate the filename.

I did a bit of research and this is what I came up with:

#include <iostream>
#include <direct.h> 

using namespace std;

int main(void)
{

char FilePath[_MAX_PATH]; 

getcwd(FilePath, _MAX_PATH);

// and just to see if getcwd worked...
cout << FilePath;

system ("pause");

}

So now that i know how to use getcwd, how do I concantinate the filename?

The location of the executable isn't required to be in the current working directory. What OS and compiler are you using?

>>how do I concantinate the filename?
strcat(FilePath,"Filename");

or

strcat(FilePath,"\\Filename");

depending on if getcwd() terminates the path with '\' or not.

commented: Perfect!! +1

If the file is in the same directory couldn't you just do this?

#include <conio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string FileName;
cout << "Enter the file name: ";
cin >> FileName;

cout << endl << endl;

ifstream in(FileName.c_str());
while (!in.eof())
{
char ch;
in.get(ch);
cout << ch;
}
getch();
return 0;
}

Example:
Enter the file name: Stuff.txt

Stuff.

That worked perfectly, thanks!! :)

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.