您的位置:首页 > 运维架构 > Linux

c++判断本地目录或本地文件是否存在linux

2015-11-27 21:17 633 查看
#include <iostream>
#ifndef _WIN32
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#endif
using namespace std;

int IsFloderexit(const char* path)
{
DIR *dp;
if ((dp = opendir(path)) == NULL)
{
return 0;
}

closedir(dp);
return -1;
}
//-1是存在
int IsFileExist(const char* path)
{
return !access(path, F_OK);
}

int main()
{
int error = IsFloderexit("/home/freetds");
if (-1 == error)
{
cout << "the floder exit" << endl;
int errorFile = IsFileExist("/home/freetds/aclocal.m4");
if (-1 == error)
{
cout << "the file exit" << endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息