How return a std::string from C's “getcwd” function

后端 未结 7 605
我寻月下人不归
我寻月下人不归 2021-01-20 07:35

Sorry to keep hammering on this, but I\'m trying to learn :). Is this any good? And yes, I care about memory leaks. I can\'t find a decent way of preallocating the char*, be

7条回答
  •  渐次进展
    2021-01-20 08:03

    When "string constructor" do everything for you:

    #include   // defines FILENAME_MAX
    #include  // for getcwd()
    
    std::string GetCurrentWorkingDir()
    {
        std::string cwd("\0",FILENAME_MAX+1);
        return getcwd(&cwd[0],cwd.capacity());
    }
    

提交回复
热议问题