How to get the path to the current file (pwd) in Linux from C?

前端 未结 5 1397
走了就别回头了
走了就别回头了 2021-02-09 16:30

I\'d like to know if it is somehow possible to run system("pwd") on the current DIR. So for example let\'s have this folder structure:

exam         


        
5条回答
  •  太阳男子
    2021-02-09 17:01

    Simply opening and reading directories does not change the current working directory. However, changing directory in your program will.

    for reference,

    #include 
    #include 
    
    int main() {
        char cwd[1024];
        chdir("/path/to/change/directory/to");
        getcwd(cwd, sizeof(cwd));
        printf("Current working dir: %s\n", cwd);
    }
    

提交回复
热议问题