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.