C + + to determine whether the file exists __c++

Source: Internet
Author: User

With function access, header files are io.h, prototypes:

int access (const char *filename, int amode);

A amode parameter of 0 indicates the existence of the check file, and if the file exists, returns 0, does not exist, returns-1.

This function can also check other file properties:

06 Check Read and Write permissions
04 Check Read permissions
02 Check Write permission
01 Check Execution Permissions
00 checking the existence of files
The experiment was successful under UNIX and VC.
The advantage is that fopen (..., "R") is not good, when no Read permission is lost.
And this file does not have Read permission, you can determine whether the file exists in the
Existence returns 0, does not exist return-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.