Differentiate between a unix directory and file in C and C++

后端 未结 4 1148

Given a path, say, /home/shree/path/def, I would want to determine if def is a directory or a file. Is there a way of achieving this in C or C++ code?

4条回答
  •  广开言路
    2020-12-05 11:22

    Alternatively you can use system() function with in built shell command "test".
    system returns the exit status of command last executed

     
     string test1 = "test -e filename" ;
     if(!system(test1))
     printf("filename exists") ;
    
    string test2 = "test -d filename" ;
     if(!system(test2))
      printf("filename is a directory") ;
    
     string test3 = "test -f filename" ;
     if(!system(test3))
      printf("filename is a normal file") ;
    
    

    but I am afraid this would work only on linux..

提交回复
热议问题