判断文件的存在和权限---C++函数

本文介绍了使用 C 语言中的 `access` 和 `waccess` 函数检查文件是否存在及是否具有读写权限的过程,并通过一个示例程序进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int _access( const char *path, int mode );

int _waccess( const wchar_t *path, int mode );


 

mode Value

Checks File For

00

Existence only

02

Write permission

04

Read permission

06

Read and write permission

 

Return Value

Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

下面是随便写了个程序测试一下。。

#include<iostream>
#include  <io.h>
#include<string>
using namespace std;

int main()
{
  

   if(access("D:\\1.txt",0)!=-1)
   {
	   cout<<"该文件存在"<<endl;
	   if(access("D:\\1.txt",2)!=-1)
	   {
			cout<<"Write Permission"<<endl;
	   }

	    if(access("D:\\1.txt",4)!=-1)
	   {
			cout<<"Read Permission"<<endl;
	   }
		 if(access("D:\\1.txt",6)!=-1)
	   {
			cout<<"Write and Read Permission"<<endl;
	   }
   }
   else
   {
	   cout<<"该文件不存在"<<endl;
   }
   return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值