Read the name of the file and check whether it is file is a directory or not.
The source code to check a given filename is a directory or not is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to check a file is a directory or not
int isDir(const char* fileName)
struct stat path;
int ret = 0;
printf("Enter filename: ");
ret = isDir(fileName);
if (ret == 0)
printf("Given file is a directory\n");
printf("Given file is not a directory\n");
Enter filename: hello.txt
Given file is not a directory
Enter filename: image
Given file is a directory
In the above program, we created two functions isDir() and main(). The isDir() function is used to check a given file is a directory or not. Here we used stat() function and S_ISREG() macro.
In the main() function, we created a character array fileName. Then we read the name from the user. Then we checked given file is a directory or not and printed the appropriate message on the console screen.
total answers (1)
start bookmarking useful questions and collections and save it into your
own study-lists, login now to start creating your own collections.