file - Portable way to check if directory exists [Windows/Linux, C] -


i check if given directory exists. know how on windows:

bool directoryexists(lpctstr szpath) {   dword dwattrib = getfileattributes(szpath);    return (dwattrib != invalid_file_attributes &&           (dwattrib & file_attribute_directory)); } 

and linux:

dir* dir = opendir("mydir"); if (dir) {     /* directory exists. */     closedir(dir); } else if (enoent == errno) {     /* directory not exist. */ } else {     /* opendir() failed other reason. */ } 

but need portable way of doing .. there way check if directory exists no matter os im using? maybe c standard library way?

i know can use preprocessors directives , call functions on different oses thats not solution im asking for.

i end this, @ least now:

#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h>  int direxists(const char *path) {     struct stat info;      if(stat( path, &info ) != 0)         return 0;     else if(info.st_mode & s_ifdir)         return 1;     else         return 0; }  int main(int argc, char **argv) {     const char *path = "./test/";     printf("%d\n", direxists(path));     return 0; } 

stat() works on linux., unix , windows well:

#include <sys/types.h> #include <sys/stat.h>  struct stat info;  if( stat( pathname, &info ) != 0 )     printf( "cannot access %s\n", pathname ); else if( info.st_mode & s_ifdir )  // s_isdir() doesn't exist on windows      printf( "%s directory\n", pathname ); else     printf( "%s no directory\n", pathname ); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -