Thread: telling what directory you're in

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    telling what directory you're in

    was wondering if there was a relatively simple way to determine what directory the program is in on the harddrive, so i could tell the user exactly where to find a file that the program creates. something like the following...

    <output to console>
    Database printed to: C:\Windows\DB Program\database.txt

    any help would be much appreciated! thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    GetCurrentDirectory(DWORD BufferSize, LPSTR Buffer);

    Duh
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    thanks for the "duh". i appreciate that...
    i'll see what i can do with what you said. thanks anyways.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    
    
    int main(int nArg,char** szArgz)
    {
    	std::cout << szArgz[0];
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    thanks a lot fordy.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Sorry, I didn't mean any offense .

    You need windows.h to use GetCurrentDirectory(). If you're doing this in DOS, include dir.h and use getcurdir(int drive, char *directory);

    EDIT: Or you could do what Fordy said, but if you change the directory during runtime that will be outdated. But I guess you wouldn't do that normally...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    thanks magos, i appreciate it.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    what would i pass as the arguemnt for "char *directory"? thanks.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <iostream>
    
    int main(int nArg,char** szArgz)
    {
      std::cout << szArgz[0];
      return 0;
    }
    This code may not work correctly. The argument vector's first element can be an empty C string. Even if it does contain the name of the program, the entire path may be omitted. The output could legally be

    C:\Windows\DB Program\database.txt

    or merely

    database.txt

    You would be better off using GetCurrentDirectory in windows.h.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by agerealm
    what would i pass as the arguemnt for "char *directory"? thanks.
    An adress to a buffer (empty char array) where the path will be stored.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    awesome, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Finding files in a directory
    By smarta_982002 in forum C Programming
    Replies: 1
    Last Post: 01-25-2008, 10:10 AM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM