您现在的位置是:首页 >

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

火烧 2022-04-21 08:39:12 1046
C++基础:怎样判断某一文件是否存在   很简单的一种办法  #i clude lt io tream gt #i clude lt f tream gt u i g ame ace td #defi

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

shell怎么判断文件是否存在 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  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码