Thread: Getting current path?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Getting current path?

    Ok, I admit, I'm a lifeless geek that is absolutely addicted to programming. Unfortunantly, that doesnt mean I'm good at it :P.

    So, I have a bit of a conundrum. I want to pick up a file in a subdirectory of where my .exe is being run from. I know I can do this:

    Code:
    ofstream hi("file.extension", extra stuff);
    But my file is not located in the same directory as my exe, its in a sub directory. So, is their some way to throw the current path into a buffer?

    Code:
    string h;
    
    h=CurrentPath();
    And what library does/would it use?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    For windows you would use GetCurrentDirectory() which is from the windows.h header file.

    It has 2 parameters, the first you use MAX_PATH.

    The second parameter is the buffer you would like to store the current directory in.

    It should look like

    GetCurrentDirectory(MAX_PATH, buffer);

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The path of program will be sent to it as the first main(int argc, char* argv[]) argument.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    If it is run from the command line it will just output the filename like "program.exe".

    However if the file is double clicked, then argv[0] will hold the current path aswell as the program name.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by John_
    However if the file is double clicked, then argv[0] will hold the current path aswell as the program name.
    In this case it will hold the full path to the exe. But that need not be the current working directory.
    You have to call somthing like GetCurrentDirectory() to find out the current working directory.
    Even if you call your exe from the commandline. You could call the exe from anywhere ( somwhere other then where the exe is located ) and your OS would locate the exe via the PATH environment variable.
    Kurt

  6. #6
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah, thank you very much!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  3. How to find the current path?
    By cloudy in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2006, 11:05 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM