trouble using getcwd

This is a discussion on trouble using getcwd within the C++ Programming forums, part of the General Programming Boards category; Nothing gets printed out. I'm unclear as to what the difference is from the return value and the buffer reference? ...

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

    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
    Registered Boozer
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    1,724
    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 human mind treats a new idea the way the body treats a strange protein; it rejects it. - P.B.Medawar

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    108
    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
    Registered Boozer
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    1,724
    Isn't it PATH_MAX ?
    The human mind treats a new idea the way the body treats a strange protein; it rejects it. - P.B.Medawar

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    108
    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
    393
    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, 01: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, 01: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

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