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


Looking at a path, say, / home / shree / path / def, I would like to define what def Is a directory or a line Is there a way to get it in C or C ++ code?

State () function to get information on the file in the following code And the S_ISDIR ('is a directory') and S_ISREG ('a regular file') macros is the rest is just an error and enough to create a completely favorable program .

  #include & lt; Stdio.h & gt; # Include & lt; Errno.h & gt; # Include & lt; Sys / stat.h & gt; Int main (int argc, char * argv []) {int position; Strait State St_buf; // sure log pass arguments. If (argc! = 2) {printf ("Usage: name & file; fileSpec> \ n"); Printf ("where is the file to check . \ N"); Return 1; } // Get the status of the file system object state = (argv [1], & amp; st_buf); If (status! = 0) {printf ("error, error =% d \ n", errno); Return 1; } // Let us know what this is after. If (S_ISREG (st_buf.st_mode)) {printf ("% s is a regular file. \ N", argv [1]); } If (S_ISDIR (st_buf.st_mode)) {printf ("% s is a directory. \ N", argv [1]); } Return 0; }  

Sample runs are shown here:

  pax> vi progName.c; GCC-A program addressing progName.c; ./progName Usage: progName where to check the file. Pax> ./progName / home / home is a directory Pax> ./progName .profile .profile is a regular file. Pax> ./progName / no_such_file Error, Error = 2  

Comments