Relative Paths

This is a discussion on Relative Paths within the C++ Programming forums, part of the General Programming Boards category; Hey all. Right now I want to find out a simple way to either get the absolute path to the ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Relative Paths

    Hey all.
    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.
    Example:
    Code:
    ifsteam file("..\\folder\\file.ext");
    is not working. I have tried using 1 dot and no dot in the beginning. This does not seem to help
    Last edited by Shingetsu Kurai; 08-06-2011 at 07:36 PM. Reason: "if" is a reserved word, changing to "file"

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    "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.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    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
    Code:
    file.is_open()
    returns false).
    I want to know the correct syntax to entering a relative file path.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    OK. I managed to make it myself. This function returns the current directory of the executable:
    Code:
    std::string engine::get_working_path()
    {
       char temp[MAX_PATH];
       return ( getcwd(temp, MAX_PATH) ? std::string( temp ) : std::string("") );
    }
    you need the headers "direct.h" and "cstdlib"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    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.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Shingetsu Kurai View Post
    OK. I managed to make it myself. This function returns the current directory of the executable:
    Code:
    std::string engine::get_working_path()
    {
       char temp[MAX_PATH];
       return ( getcwd(temp, MAX_PATH) ? std::string( temp ) : std::string("") );
    }
    you need the headers "direct.h" and "cstdlib"
    So the solution of your problem has no relationship to the question you asked.

    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.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Force relative paths
    By aydinh in forum C Programming
    Replies: 3
    Last Post: 01-13-2011, 10:43 AM
  2. exec with system(..), how to use relative paths?
    By sept in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2008, 01:22 PM
  3. relative paths with autotools
    By zxcv in forum Tech Board
    Replies: 1
    Last Post: 02-28-2008, 10:18 AM
  4. boost::filesystem - relative paths
    By azteched in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2005, 05:24 AM
  5. using relative file paths..?
    By anomaly in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21