C ++ (5) access function to determine whether a file exists

Source: Internet
Author: User
Recently, I saw a function. First, I think it is very sb, because the definition of remove is inside the if, and it becomes a local variable. The result is that if the file "234. bin "if it does not exist, an error will occur, because the lifecycle of the remove operation is limited. Result: # includeiostream # includeunistd. h # includestdio. h # includestdlib. husingna

Recently, I saw a function. First, I think it is very sb, because the definition of remove is inside the if, and it becomes a local variable. The result is that if the file "234. bin "if it does not exist, an error will occur, because the lifecycle of the remove operation is limited. Result: # includeiostream # include unistd. h # include stdio. h # include stdlib. husing na

Recently, I saw a function. At first glance, I thought it was very sb, because the definition of remove is inside the if, and it becomes a local variable. The result is if the file "234. bin "if it does not exist, an error will occur, because the lifecycle of the remove operation is limited.

As a result, my eyes are highlighted:

# Include
 
  
# Include "unistd. h "# include" stdio. h "# include" stdlib. h "using namespace std; int main () {if (access (" 234.bin", F_ OK) {bool remove = true;} if (remove) {cout <"Need to delete... "<
  
   As a result, no matter whether the file exists or not:
   


In fact, I personally think this problem lies in the return value of this access function. Its return value is

0 if the file is in the specified mode

-1 if an error occurs

Therefore, whether the above program finds the file (0) or cannot find (-1), it is false, so it should never enter if (remove ..

So it should be:

if(0 == access("234.bin",F_OK)){remove = true;}
After this change, I still cannot see the error I want. I want to see the error that does not exist in remove ~~ Unfortunately, it is still:

Originally:

Remove is an existing function. The function address is not empty, so you can always enter if (remove ){}

You can use the following code to determine whether a file exists:

# Include
    
     
# Include "unistd. h "# include" stdio. h "# include" stdlib. h "using namespace std; int file_exist (char * file) {return (access (file, F_ OK) = 0);} int main () {cout <"Does file exist:" <(file_exist ("234.bin ")? "Yes": "No") <
     
      Summary:
      

(1) Use the access function to note that the returned values are 0 and-1, both of which are false.

(2) remove is a function name. Do not use the system stuff when defining the name.

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.