Output file where executable is located C++ [duplicate] - c++

This question already has answers here:
Get path of executable
(25 answers)
How do I get the directory that a program is running from?
(26 answers)
Finding current executable's path without /proc/self/exe
(14 answers)
Closed 2 years ago.
Is there a way to set the output directory of a file in the same one where the executable is located and not where the executable is executed?
ofstream outputFile;
outputFile.open("text.txt");
outputFile<<"test";
When I click on the .exe it typically outputs the file in the same directory I started it but when I execute it in different ways say for example with a .BAT file, the output directory is automatically set where I started the .BAT file (not where the .exe is located)

Related

How can I run an executable and take input from a text file on Linux? [duplicate]

This question already exists:
Command line parameters c++ [duplicate]
Closed 3 years ago.
How can I run an executable and take input from a text file on Linux?
I was trying to use:
./(name of my executable) TextFile.txt but it does not work.
you can do ./(name of exe) < input.txt
and if you want to write the output to a text file you can do:
./(name of exe) > output.txt

Get file of current macro file in CMake [duplicate]

This question already has answers here:
In CMake, how can I find the directory of an included file?
(4 answers)
Closed 4 years ago.
I'm writing a .cmake file that handles a bunch of stuff and needs access to an executable in a fixed relative path to the .cmake file. In this instance, the orgianization looks like this:
CMakeLists.txt
- cmake
- tools.cmake
- bin
- exectuable.exe
The issue is that tools.cmake is included using include and thus, CMAKE_CURRENT_SOURCE_DIR returns the directory of the CMakeLists.txt. The reason I don't want to hardcode the path is that I want to put the tools.cmake and binary in a separate GIT repository and I don't want to force the user to place it in some specific folder.
Only solution I can come up with right now is to supply a function that sets and stores the path to tools.cmake. That's not really elegant.
Answered here: https://stackoverflow.com/a/12854575/2532768
CMAKE_JUCE_TOOLS_PATHdoes not return the correct path within functions or macros in tools.cmake but it results in the correct path outside any function. So one can us
set(PATH_TO_TOOLS_CMAKE ${CMAKE_JUCE_TOOLS_PATH})
to store the path while it can be accessed in a variable and than access PATH_TO_TOOLS_CMAKE in functions and macros

Using 'rename' command for all files in subfolders [duplicate]

This question already has answers here:
Rename files and directories recursively under ubuntu /bash
(8 answers)
Closed 5 years ago.
I am using this command to replace a word with another in many filenames (batch):
rename 's/oldname/newname/g' **
But it works for the current open folder only. I am not sure how to make it work for that directory and all the sub-directories.
Thanks.
Mike,
Try this: Create new folders, move files, delete old folder. Then, move over directories.
Regards

Eclipse c++ debugging executable is not finding the file with relative path [duplicate]

This question already has answers here:
how to access a text file while debugging with eclipse CDT
(5 answers)
Closed 6 years ago.
I have a project that has more makefiles. At some specific folders, there are some makefiles that can create binaries/executables. I right-click on the makefile and do a Make Tarkgets -> Build -> all, so I get the executable right there. If I run the executable from console, it works fine, but if I do a right-click on it and Debug as -> Local C/C++ Application, it runs but at some point it is reading a file with relative path:
FILE *f = fopen(fName, "rb"); // fname is a relative path : ../../../path/to/file
if(f == NULL)
{
perror(fName);
exit(2);
}
and it is not finding it; the pointer is empty/NULL. To me it seems like Eclipse is searching for the file from the place where the project was opened, not from where the executable is. I suppose this because if I create a project from the makefile that created the executable, it works, it finds the file. But from that place I cannot see the sources, so, for placing breakpoints I have to run step-in by step-in until I get in the right place/right source.
Can anyone help me to fix this thing? Is there a way to make it search the file from the place where the executable is? Thanks
P.S. : I've seen this post, it's similar, but it did not pointed me to anything...
I guess work directory is different for both cases, and thats why you can not open your file. Try to use absolute path for fopen() or look for a way to specify proper work directory for the second case.
There is a way to modify the working directory, as mentioned in this answer:
right-click on binary/executable Debug As -> Debug Configuration ...
in Arguments tab, in the bottom, there is a Working directory: area, uncheck the Use default and add the correct path (where is the executable/binary). The buttons Workspace... and File System... may help

Meaning of ./main command in terminal [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 7 years ago.
I am working with this c++ project for making command line text editor like Vim. The documentation tells that in order to run this text editor in terminal you need to write command ./main in the project folder.
The project folder has main.cpp file. Is this a command to execute that file (may be I am wrong) or this command is a standard terminal command.
Thank you.
Running ./main means run main from the current directory (.).
The current directory is normally missing from $PATH so you have to specify it explicitly.