C/C++判断文件是否存在
2015-12-24 22:48
477 查看
C/C++判断文件是否存在
The following is the most common method for checking if file exist:
This method works on ALL platforms I have used (Windows, Unix, Linux).
And it does not require opening the file, which could cause problems on some platforms if the file has protected property settings.
This method works both in C and in C++.
Although stat is not part of the standard, it is supported by all major platforms and compilers
C/C++判断文件是否存在
The following is the most common method for checking if file exist:
#include <sys/stat.h> bool FileExist(const char* FileName) { struct stat my_stat; return (stat(FileName, &my_stat) == 0); } //Example Usage int main(int argc, char* argv[]) { bool v1 = FileExist("c:\\autoexec.bat"); bool v2 = FileExist("c:\\nofile.bat"); bool v3 = FileExist("c:\\config.sys"); bool v4 = FileExist("c:\\nofile2.bat"); return 0; }
This method works on ALL platforms I have used (Windows, Unix, Linux).
And it does not require opening the file, which could cause problems on some platforms if the file has protected property settings.
This method works both in C and in C++.
Although stat is not part of the standard, it is supported by all major platforms and compilers
相关文章推荐
- 2015/12/24:嵌入式C语言的位操作随笔
- 2015/12/24:嵌入式C语言的位操作随笔
- 2015/12/24:嵌入式C语言的位操作随笔
- Leetcode 8 - String to Integer (atoi)
- 一起talk C栗子吧(第八十二回:C语言实例--简单圣诞树)
- 算术表达式语法分析
- C语言变量
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- 极客必备之C++ 定时器
- C++为什么类的成员函数中(复制构造函数,赋值操作符等)可以有貌似类的对象访问类的私有成员的用法?
- 黑马程序员--C语言自学笔记---15C语言版通讯录
- 黑马程序员--C语言自学笔记---14static、extern、文件
- c语言学习(2)
- Leetcode 7 - Reverse Integer
- C++笔记——多态练习
- 黑马程序员--C语言自学笔记---13结构体、预编译、宏、条件编译
- POJ1035——Spell checker
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)