Right now I want to find out a simple way to either get the absolute path to the exe, or to put a relative path for a fstream.
is not working. I have tried using 1 dot and no dot in the beginning. This does not seem to help
"if" is a reserved keyword, so cannot be used as a variable name. So the line of code you have given is not even guaranteed to compile.
If you have some other notion "not working" (or, for that matter, "working") you need to specify it. The information you have given is woefully insufficient for anyone (other than that rare species known as mind readers) to divine what your problem even is.
I am trying to get a file open using a relative file path.
I was using "if" as an example (which was as I now see a mistake). I will use file from here on to reference to it.
The problem is that the file does not open (that is to say that after this statement
I want to know the correct syntax to entering a relative file path.
OK. I managed to make it myself. This function returns the current directory of the executable:
you need the headers "direct.h" and "cstdlib"
return ( getcwd(temp, MAX_PATH) ? std::string( temp ) : std::string("") );
Assuming your target system is windows, then "..\\folder\\file.ext" is a relative path. If your target system is not windows, then your way of specifying paths is invalid.
Go through the basics
1) Check that the current working directory (determined at run time) exists and is the directory you think it is.
2) Check that the current working directory actually has a parent. (the root of a disk, such as "C:\", does not have a parent)
3) Check that the parent actually has a directory named "folder".
4) Check that directory actually contains a file named "file.ext"
5) Check that all of those directories and files, if they exist, are able to be opened (for example, permissions are set appropriately).
If any of those steps fail, then that definitively explains why the file cannot be opened.
There are also some invalid assumptions you are probably making, and you need to check those.
1) Input files (and the directories that contain them) are not magically created simply by the act of attempting to open them.
2) If you are running your program within an IDE, it often has a different working directory from what you might expect
3) If the filename is a string (read from a file, or from the user) backslashes should not be in pairs. The "two backslashes to get one" rule is only valid in string literals within C/C++ source code.
So the solution of your problem has no relationship to the question you asked.
Originally Posted by Shingetsu Kurai
You asked about how to specify filenames using relative paths, and while opening failed when you used a relative path. Not about how to obtain the current working directory (except as an alternative, which was not relevant in the example you showed).
In future, try to actually ask a question that is relevant to the answer you seek.
Note that getcwd() is not standard C++.
People here are not mind readers, so put more effort into asking clear questions in future.