检查便携式方式,如果目录存在的[Windows / Linux上,C] [英] Portable way to check if directory exists [Windows/Linux, C]

查看:172
本文介绍了检查便携式方式,如果目录存在的[Windows / Linux上,C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果给定的目录是否存在。我知道如何做到这一点在Windows上:

  BOOL DirectoryExists(LPCTSTR szPath)
{
  DWORD dwAttrib = GetFileAttributes(szPath);  回报(dwAttrib = INVALID_FILE_ATTRIBUTES和放大器;!&安培;
         (dwAttrib&放大器; FILE_ATTRIBUTE_DIRECTORY));
}

和Linux的:

  DIR * DIR =执行opendir(MYDIR);
如果(DIR)
{
    / *目录中。 * /
    closedir(DIR);
}
否则,如果(ENOENT ==错误号)
{
    / *目录不存在。 * /
}
其他
{
    / *执行opendir()失败某些其他原因。 * /
}

但我需要这样的便携方式..有什么办法,以检查是否存在一个目录不管是什么操作系统即时通讯使用?也许C标准库的方法吗?

我知道,我可以使用preprocessors指令,并呼吁不同的操作系统这些功能,但是那不是解决林要求。

我结束了这一点,至少现在是这样:

 的#include< SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释dirExists(为const char *路径)
{
    结构统计信息;    如果(统计(路径,及放大器;!信息)= 0)
        返回0;
    否则,如果(info.st_mode&安培; S_IFDIR)
        返回1;
    其他
        返回0;
}INT主(INT ARGC,字符** argv的)
{
    为const char *路径=./TEST/;
    的printf(%d个\\ N,dirExists(路径));
    返回0;
}


解决方案

STAT()在Linux上工作。 ,UNIX和Windows,以及:

 的#include< SYS / types.h中>
#包括LT&; SYS / stat.h>结构统计信息;如果(STAT(路径,和放大器;!资讯)= 0)
    的printf(不能访问%S \\ n,路径名);
否则,如果(info.st_mode&安培; S_IFDIR)// S_ISDIR()没有在我的Windows存在
    的printf(%s是一个目录\\ n,路径名);
其他
    的printf(%s是没有目录\\ n,路径名);

I would like to check if a given directory exists. I know how to do this 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 does not exist. */
}
else
{
    /* opendir() failed for some other reason. */
}

But I need a portable way of doing this .. Is there any way to check if a directory exists no matter what OS Im using? Maybe C standard library way?

I know that I can use preprocessors directives and call those functions on different OSes but thats not the solution Im asking for.

I END UP WITH THIS, AT LEAST FOR 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 and Windows as 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 my windows 
    printf( "%s is a directory\n", pathname );
else
    printf( "%s is no directory\n", pathname );

这篇关于检查便携式方式,如果目录存在的[Windows / Linux上,C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆