I am trying to compile a binary file that will open a txt file in the same directory that it is in next to it, read its components, store the first word into a string, and print it out. Here is the code:
#include <iostream>
#include <fstream>
int main(){
std::ifstream is;
is.open("text.txt");
std::string s;
if(is.fail()){
std::cout << "Failed to open file" << std::endl;
return 1;
}
is >> s;
is.close();
std::cout << "text gotten from file: " << s << std::endl;
return 0;
}
my build command: clang++ main.cpp -o out
I am compiling this code with clang on Mac OS X Big Sur. This code compiles fine but the issue is that it will only read the txt file if ran from the command prompt with ./out
. My goal is to make it so that if I double click on the exe the binary file will run in the directory that it is in. By default for some reason the directory that an application is run from on Mac OS is the home directory. I know this because I tried using an output stream instead of the input stream and it created the file under ~/ So my question is:
Is there a way to make an executable binary run from the file that is in when double clicked?