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?

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?



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;
cout << ch;
return 0;

Enter the file name: Stuff.txt


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.