Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-01-2004, 02:18 AM   #1
LQ Newbie
Registered: Jul 2004
Posts: 23

Rep: Reputation: 15
how to distinguish a file and a directory


I need to know how to distinguish between a file and a directory in a C program,
under Linux and Windows.
Is there any system calls to check if it is a directory?

Old 10-01-2004, 03:50 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 68
Try and open it using opendir:

#include <sys/types.h>
#include <dirent.h>
#incllude <errno.h>

//Returns 0 for directory, 1 for file, -1 for error in errno
//str is the pathname of the unknown file/directory
int isDirectory(char * str) {
  DIR dir;
  errno = 0;
  dir = opendir(str);
  if (dir != NULL) {
     return 0;
  } else if (errno == ENOTDIR)
     return 1;
  } else return -1;
Old 10-01-2004, 04:21 AM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
You can check the file type (regular file, dir, pipe, device, etc.) with stat() (see "man 2 stat").
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char **argv)
     char *filename;
     int result;
     struct stat statinfo;

     if (argc != 2) {
		  fprintf(stderr, "Usage: %s [<file>]\n", argv[0]);
		  return 1;
     filename = argv[1];
     result = lstat(filename, &statinfo);
     if (result < 0) {
		  if (errno == ENOENT) {
			   printf("File %s does not exist\n", filename);
			   return 0;
		  } else {
			   return 1;
     printf("File %s exists, and is a ", filename);
     switch (statinfo.st_mode & S_IFMT) {
     case S_IFREG:  printf("regular file.\n"); break;
     case S_IFSOCK: printf("filesystem socket.\n"); break;
     case S_IFLNK:  printf("symbolic link.\n"); break;
     case S_IFBLK:  printf("block device file.\n"); break;
     case S_IFDIR:  printf("directory.\n"); break;
     case S_IFCHR:  printf("character device file.\n"); break;
     case S_IFIFO:  printf("filesystem pipe (fifo) file.\n"); break;
     default:  printf("unknown type of file.\n"); break;
     return 0;
Note: Here lstat() is used instead of stat(), in order to be able to detect symbolic links.

Last edited by Hko; 10-01-2004 at 04:24 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Distinguish between crash and reboot tukaaa Linux - General 4 11-26-2005 05:25 PM
distinguish load and store from segmentation fault gaven Programming 1 08-24-2005 03:40 PM
distinguish load and store from segmentation fault gaven Linux - Software 0 08-23-2005 01:36 PM
how to distinguish kernel versions fobius Linux - General 2 06-24-2004 07:28 AM
How can I distinguish a whole mail from TCP packets sunnyriver Linux - Networking 2 03-22-2004 11:08 PM

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration