Determine whether a path exists in Linux (whether a folder exists)

Source: Internet
Author: User

# Include <iostream>
# Include <sys/types. h>
# Include <dirent. h>
# Include <sys/STAT. h>
# Include <unistd. h>
Using namespace STD;

Int main (void)
{
Char buff [] = "/home/WHC/Test2 /";
// 1 opendir () and closedir
Dir * dir = NULL;
Dir = opendir (buff );
If (null = DIR)
Cout <"1: The file does not exist" <Endl;
Else
{
Cout <"1: file exists" <Endl;
Closedir (DIR );
}
 
// 2 Access
If (access (buff, f_ OK) = 0)
Cout <"2: file exists" <Endl;
Else
Cout <"2: The file does not exist" <Endl;
 
// 3 stat
Struct stat st;
If (STAT (buff, & St) = 0)
Cout <"3: file exists" <Endl;
Else
Cout <"3: The file does not exist" <Endl;

Return 0;
}

 

The second method is relatively simple and convenient.

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.