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?

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.


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


This question has already been answered. Start a new discussion instead.