shell怎么判断文件是否存在 C++基础:怎样判断某一文件是否存在
C++基础:怎样判断某一文件是否存在

很简单的一种办法
#include <iostream> #include <fstream> using namespace std; #define FILENAME stat dat int main() { fstream _file; _file open(FILENAME ios::in); if(!_file) { cout<<FILENAME<< 没有被创建 ; } else { cout<<FILENAME<< 已经存在 ; } return ; }
另外一种利用 c 语言的库的办法
函数名: Access 功 能: 确定文件的访问权限 用 法: int access(const char *filename int amode); 程序例: #include <stdio h> #include <io h>
int file_exists(char *filename);
int main(void) { printf( Does NOTEXIST FIL exist: %sn file_exists( NOTEXISTS FIL ) ? YES : NO ); return ; }
int file_exists(char *filename) { return (access(filename ) == ); }
access(filename ) 表示判断文件是否存在
finename 文件名称 mode 模式 共 种模式
检查文件是否存在
检查文件是否可运行
检查文件是否可写访问
检查文件是否可读访问
lishixinzhi/Article/program/c/201311/11109