C language to determine whether a folder or file exists method "Go"

Source: Internet
Author: User

C Language Method method for judging whether a folder or file exists: Access function determines whether a folder or file exists

Function prototypes: int access (const char *filename, int mode);

Owning header file: io.h

FileName: can fill in folder path or file path

mode:0 (F_OK) only determines if there is

2 (R_OK) to determine write permissions

4 (W_OK) to determine Read permissions

6 (X_OK) Judge execution permissions

To determine if a folder exists, mode takes 0 and determines if a file exists, mode can take 0, 2, 4, 6. The return value is 0 if it exists or has permission, no or no permission, and the return value is-1.

Error code

The file specified by the eaccess parameter pathname does not meet the permissions required for the test.

Erofs files to test write permissions exist in the read-only file system.

The Efault parameter pathname pointer is out of the accessible memory space.

The EINVAL parameter mode is incorrect.

Enametoolong parameter pathname too long.

The Enotdir parameter pathname is a directory.

Enomem Core memory is low

The Eloop parameter pathname has too many symbolic connection problems.

EIO I/O access error.

Special CAUTION: Use Access () for user authentication to be particularly cautious, such as an empty file after access () to open () may cause problems with the system security.

Instance:

#include <stdio.h>

#include <io.h>

int main (void)

{

if (!access ("C://windows", 0))

Puts ("C://windows exisits!");

Else

Puts ("C://windows doesn ' T exisit!");

return 0;

}

Method Two: fopen function to determine whether a file exists

Function prototypes: FILE *fopen (char *filename, char *type);

FileName: File path

Type: How to open a file (R, W, r+, w+, A, RB, WB, etc.)

Used to determine if a file exists, R or RB can be used, because the file may be created automatically if other means are used. The return value is null (cannot be opened) and a positive number (can be opened).

A special reminder: the judgment in this way is not entirely correct, because some files exist, but may not be readable.

C language to determine whether a folder or file exists method "Go"

Related Article

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.