Thread: trouble using getcwd

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    trouble using getcwd

    Nothing gets printed out. I'm unclear as to what the difference is from the return value and the buffer reference?
    Code:
        char buffer[200];
        char *startingPath = getcwd(buffer, sizeof(unsigned long));
        cout << "Here is path: " << startingPath << endl;
    I'm on Windows 7.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It didn't work because you (inexplicably!) passed sizeof(unsigned long) as the buffer size, which is either the number 4 or 8. Since that was presumably not enough to hold the directory name, it didn't work (i.e., it returned NULL to indicate this).

    Code:
    char buffer[200];
    
    if (getcwd(buffer, sizeof(buffer)) == NULL) {
        cerr << "getcwd failed\n";
        exit(1);
    }
    
    cout << "Here is path: " << buffer << endl;
    The return value is the same as buffer, unless buffer is NULL, in which case the function allocates a properly sized array for you.
    Code:
    char *buf = getcwd(NULL, 0);
    // ... use buf
    free(buf); // be sure to free it
    getcwd(3): current working directory - Linux man page
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    I changed unsigned long to MAX_PATH but the macro is not found. I tried including limits.h but no luck. Which library is the MAX_PATH defined in?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Isn't it PATH_MAX ?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    I got it working, it was MAX_PATH in the windows.h library. Is there a more platform independent solution?
    Last edited by c_weed; 08-16-2012 at 12:23 PM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    stdio only defines FILENAME_MAX for the file primitives.
    Posix defines PATH_MAX and windows apparently defines MAX_PATH, as getcwd() is platform specific anyway, the associated limits are platform specific too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More trouble
    By Metalix in forum C Programming
    Replies: 12
    Last Post: 01-21-2005, 02:18 PM
  2. getcwd() function messes up my environment variable
    By miclus in forum C Programming
    Replies: 5
    Last Post: 09-20-2004, 06:33 PM
  3. getcwd( )
    By cigcode in forum C Programming
    Replies: 3
    Last Post: 02-28-2004, 02:45 AM
  4. big trouble in c++!!!
    By newbie_grg in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 04:18 PM
  5. trouble with dev c++
    By Klinerr1 in forum C Programming
    Replies: 6
    Last Post: 06-14-2002, 11:14 PM