zoukankan      html  css  js  c++  java
  • C语言判断文件是否存在

    用函数access,头文件是io.h,原型:    int   access(const   char   *filename,   int   amode);

    amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。

    这个函数还可以检查其它文件属性:

    06     检查读写权限 04     检查读权限 02     检查写权限 01     检查执行权限 00     检查文件的存在性 在UNIX和VC下实验成功。 好处是 fopen(..,"r")不好,当无读权限时一不行了。 而这个就算这个文件没有读权限,也可以判断这个文件存在于否 存在返回0,不存在返回-1 #include <stdio.h> int main() {        printf ("%d",access("111",0));

    --------------------------------------------------------------------------------------------

    #include <io.h> #include <stdio.h> #include <stdlib.h>

    void main( void ) {    /* Check for existence */    if( (_access( "ACCESS.C", 0 )) != -1 )    {       printf( "File ACCESS.C exists\n" );       /* Check for write permission */       if( (_access( "ACCESS.C", 2 )) != -1 )          printf( "File ACCESS.C has write permission\n" );    } }

    Output

    File ACCESS.C exists File ACCESS.C has write permission

  • 相关阅读:
    IOS 消息分发
    使用do{ } while(0)的好处
    BdAsyncTask学习
    用户体验学习笔记(工程中发现的PM常犯错误)
    Xcode 7 调试野指针利器 Address sanitizer
    Xcode磁盘空间大清理
    xcode:关于Other Linker Flags
    mac 下打开多个Eclipse
    shape 代码生成器
    查看APK方法数的工具dex-method-counts
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/3068666.html
Copyright © 2011-2022 走看看