C. Determine whether a file exists

Source: Internet
Author: User

I forgot what I saw there. Write it here and record it.

 

1) Access
2) fopen
3) stat fstat?

>>>>>
Function access should be used. The header file is Io. H. prototype:

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

If the amode parameter is 0, the object existence is checked. If the object exists, 0 is returned. If the object does not exist,-1 is returned.

This function can also check other file attributes:

06 check read/write permissions
04 check read permission
02 check write permission
01 check the execution permission
00 check file existence

Int access (const char * path, int amode );

If the requested access is permitted, access () succeeds and shall return 0; otherwise,-1 shall be returned and errno shall be set to indicate the error.

Check whether the file exists: Access (filename, 0)
If 0 is returned, the object exists; otherwise, the object does not exist.

>>>>>
Both access and stat are supported. In fact, stat is a more underlying function. Generally, OS calls it as a system call. All functions, including access and fopen, call stat to check whether the file permissions match those of program executors. This is especially true in operating systems such as Linux and UNIX.

>>>>>
Int is_file_exist (const char * file_path)
{
Struct stat stat_buf;

If (lstat (file_path, & stat_buf) <0)
Return-1;

If (0 = s_isreg (stat_buf.st_mode ))
Return-1;

Return 0;
}

>>>>>
Example

/* Access. C: This example uses _ access to check
* File named "access. c" to see if it exists and if
* Writing is allowed.
*/

# 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

>>>>>
# Include

Int main (INT argc, char ** argv)
{
Int rt_fi;
Int II;
Char flnm [1023];

For (II = 0; II strcpy (flnm, argv [1]);
Rt_fi = access (flnm, f_ OK );
If (rt_fi = 0) {printf ("file of % s is exist. \ n", flnm );}
Else {printf ("file of % s is not exist! \ N ", flnm );}
Return (0 );
}

>>>>>
File * FP;
If (FP = fopen ("C: \ a.txt", "rb") = NULL)
{
Cout <"the file is not exist! \ N ";
Return 0;
}

 

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.