What is a cross-platform way to get the current directory?

前端 未结 3 1459
孤街浪徒
孤街浪徒 2020-12-05 23:55

I need a cross-platform way to get the current working directory (yes, getcwd does what I want). I thought this might do the trick:

#ifdef _WIN32
    #includ         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-06 00:49

    You cannot call getcwd with a NULL buffer. As per the Opengroup:

    If buf is a null pointer, the behavior of getcwd() is unspecified.

    Also, getcwd can return NULL which can break a string constructor.

    You'll need to change that to something like:

    char buffer[SIZE];
    char *answer = getcwd(buffer, sizeof(buffer));
    string s_cwd;
    if (answer)
    {
        s_cwd = answer;
    }
    

提交回复
热议问题