I am creating a game engine in C++, and I had started adding support for loading files. I created a load file method that looks like this:
#include <string>
#include <fstream>
std::string read_file(const char* filepath) {
FILE* file = fopen(filepath, "rt");
if(file != NULL) {
fseek(file, 0, SEEK_END);
} else {
std::string error_message = std::string("[ERROR]: Failed to load file ");
error_message.append(filepath);
return error_message;
}
unsigned long length = ftell(file);
char* data = new char[length + 1];
memset(data, 0, length + 1);
fseek(file, 0, SEEK_SET);
fread(data, 1, length, file);
fclose(file);
std::string result(data);
delete[] data;
return result;
}
To test this function, I decided to try to load a "test.txt" file, but this will not work unless I include the full path, e.g.
"/Users/(Username)/.../(Executable Directory)/text.txt"
instead of just
"test.txt"
So, to fix this, I need a way to get the full path of the directory that the executable file is in, and then append the file name onto the end of the path, and load that in my load file method.
Anyone know how to get the path?