请问在C里面如何实现判断某个目录是否存在?急!!!在线,谢谢!

how_warm 2004-08-09 08:08:47
请问在C里面如何实现判断某个目录存在?急!!!在线,谢谢!
...全文
313 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ckc 2004-08-09
  • 打赏
  • 举报
回复
stat函数
庄鱼 2004-08-09
  • 打赏
  • 举报
回复
一楼的方法很简单的,说明在dir.h文件里,再c++里控制台方式下,还有一个函数是_getdrive,说明在direct.h里
  • 打赏
  • 举报
回复
access
Synopsis
#include <io.h>
int _access(const char *path,int mode) ;
Description
The access function, when used with files, determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; since under Windows all directories have read and write access.
The mode argument can be one of :
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission

Returns
Zero if the file has the given mode, -1 if an error occurs.

Portability :
Windows. Under Unix a similar function exists too.
Note that lcc-win32 accepts both _access (Microsoft convention) and access.

willpower88 2004-08-09
  • 打赏
  • 举报
回复
Sorry, is C++.
willpower88 2004-08-09
  • 打赏
  • 举报
回复
ofstream out;
out.open("xxx");
if(xxx)
...
  • 打赏
  • 举报
回复
以上为TC.2.0中。
下面举一个在C++ Builder中的方法:
判断目录是否存在:

C++ Builder中提供了检查文件是否存在的函数FileExists,但没有提供检查目录是否存在的函数,我们可以用Windows API函数FindFirstFile实现这个功能。程序实现如下:

设char *Dir为带判断的目录
bool Exist; // 最后结果,表示目录是否存在
if(Dir[strlen(Dir)]=='\')Dir[strlen(Dir)-1]=''; // 先删除最后的“”
WIN32_FIND_DATA wfd; // 查找
HANDLE hFind=FindFirstFile(Dir,&wfd);
if(hFind==INVALID_HANDLE_VALUE)Exist=false; // 没有找到配备,目录肯定不存在
else
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // 检查找到的结果是否目录
Exist=true; // 是目录,目录存在
else
Exist=false; // 是目录,目录不存在
FindClose(hFind);
}
  • 打赏
  • 举报
回复
int access(char *filename,int amode)
本函数检查文件filename并返回文件的属性, 函数将属性存于amode中,amode由以下位的组合构成
06可以读、写 04可以读 02可以写 01执行(忽略的) 00文件存在
如果filename是一个目录,函数将只确定目录是否存在函数执行成功返回0,否则返回-1
  • 打赏
  • 举报
回复
利用这两个函数:
====================================================================

int findfirst(char *pathname,struct ffblk *ffblk,int attrib)
查找指定的文件,成功返回0
pathname为指定的目录名和文件名,如"C:\\WPS\\TXT"
ffblk为指定的保存文件信息的一个结构,定义如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk ┃
┃{ ┃
┃ char ff_reserved[21]; /*DOS保留字*/┃
┃ char ff_attrib; /*文件属性*/ ┃
┃ int ff_ftime; /*文件时间*/ ┃
┃ int ff_fdate; /*文件日期*/ ┃
┃ long ff_fsize; /*文件长度*/ ┃
┃ char ff_name[13]; /*文件名*/ ┃
┃} ┃
┗━━━━━━━━━━━━━━━━━━┛
attrib为文件属性,由以下字符代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只读文件┃FA_LABEL 卷标号┃
┃FA_HIDDEN 隐藏文件┃FA_DIREC 目录 ┃
┃FA_SYSTEM 系统文件┃FA_ARCH 档案 ┃
┗━━━━━━━━━┻━━━━━━━━┛
例:
struct ffblk ff;
findfirst("*.wps",&ff,FA_RDONLY);

int findnext(struct ffblk *ffblk) 取匹配finddirst的文件,成功返回0

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧