How to check if file exists?

I am reading from files and I want them to be stored in the same location as the program's .ccp file. Since this could change based on where the user puts the application folder, I'm doing the following code to get this to work in Visual C++.

However, I want to add a file name to the end of dir. I have tried putting dir into a stringstream and regular string but no luck yet.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
#include <string>
#include <direct.h>
using namespace std;
int main()
{
  char * dir = getcwd(NULL, 0);
//stringstream ss;
//ss << dir;
//printf("Current dir: %s", ss);
  printf("Current dir: %s", dir);
	system("pause");
	return 0;
}
Last edited on
Ok.. I figured it out.. BUT...

how do I check to see if the file exists? I do NOT want to edit the file. It's being loaded into a font class, I just want to verify it exists in the project folder.

So far (working)...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
#include <string>
#include <direct.h>
using namespace std;
int main()
{
  char * dir = getcwd(NULL, 0);
	stringstream s;
	s << dir << "\\arial.ttf";
	cout << s.str();

       //Checks to see if s.str() is a valid path name??

	system("pause");
	return 0;
}
1
2
3
4
#include <fstream>

inline bool file_exists_and_can_be_read_from( const char* path_to_file )
{ return std::ifstream(path_to_file) ; }

Topic archived. No new replies allowed.