http://blog.csdn.net/zjl_1026_2001/article/details/6587320
Everyone has a different approach to determining whether a file exists. I usually take two approaches: open and access;
There are similarities and differences between the two methods, and here is a detailed explanation of their differences:
Open is a function of opening, reading and writing, and so on, access is a function of determining file permissions. Under Linux, because the file has different permissions, when the current user does not have the right to read this file, used to determine whether the file exists, obviously inappropriate. and access can do it.
The first argument to open is the file path, the second parameter is the Open file option, the common o_rdonly (read), O_wronly (write), O_RDWR (read-write), O_creat (create). The third parameter is usually used when creating a file, which is used to set permissions for the file.
The first parameter in Access is the file path, and the second parameter is the mode of the test. Commonly used have R_OK: Test Read permission, W_OK: Test Write permission, X_OK: Test execution permissions, F_OK: Test file exists;
Let me make an example of writing a small program to see the difference between the two.
Test sequencing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main (int argc, char **argv)
{
Char path[50];
memset (path,0,50);
sprintf (Path, "%s", "./test");
int FD;
printf ("Access function-------------------------/n");
if (FD = Access (PATH,F_OK)) = = 0)
printf ("[Access] file exist!/n");
Else
printf ([Access] File not exist!! /n ");
if (FD = Access (PATH,R_OK)) = = 0)
printf ("[Access] Read file ok!/n");
Else
printf ("[Access] Read file no!/n");
if (FD = Access (PATH,W_OK)) = = 0)
printf ("[Access] Write file ok!/n");
Else
printf ("[Access] Write file no!/n");
if (FD = Access (PATH,X_OK)) = = 0)
printf ("[Access] Execute file ok!/n");
Else
printf ("[Access] Execute file no!/n");
printf ("Open function-------------------------/n");
if (fd = open (path,o_rdonly)) = = 1)
printf ("[Open] open no!/n");
Else
{
Close (FD);
printf ("[Open] open ok!/n");
}
if (fd = open (path,o_wronly)) = = 1)
printf ("[Open] Write no!/n");
Else
{
Close (FD);
printf ("[Open] Write ok!/n");
}
if (fd = open (path,o_rdwr)) = = 1)
printf ("[Open] Rdwr no!/n");
Else
{
Close (FD);
printf ("[Open] Rdwr ok!/n");
}
}
First we create a normal file test, modify the file permission is 0744, modify the file owner for root. We operate under the normal user MPEG4.
[mpeg4@mc2800 file1]$ sudo chown root:root test
[mpeg4@mc2800 file1]$ sudo chmod 0744 test
[Mpeg4@mc2800 file1]$ Ls-l
Total 20
-RWXRWXRWX 1 MPEG4 mpeg4 7157 2009-09-17 09:37 file
-RWXRWXRWX 1 MPEG4 mpeg4 991 2009-09-17 09:44 file.c
-RWXRWXRWX 1 MPEG4 mpeg4 2009-09-17 08:56 Makefile
-RWXRWXRWX 1 MPEG4 mpeg4 3808 2009-08-20 13:52 Records
-rwxr--r--1 root 0 2009-09-17 10:06 test
Well, we run my program to see the results:
[mpeg4@mc2800 file1]$./file
Access function-------------------------
[Access] File exist!
[Access] Read file ok!
[Access] Write file no!
[Access] Execute file no!
Open function-------------------------
[Open] Open ok!
[Open] Write no!
[Open] Rdwr no!
Obviously, when the user has Read permission, use that function to determine whether the file exists.
Now we change the file permissions to 0740:
sudo chmod 0740 test
Look at the test results again:
[mpeg4@mc2800 file1]$./file
Access function-------------------------
[Access] File exist!
[Access] Read file no!
[Access] Write file no!
[Access] Execute file no!
Open function-------------------------
[Open] Open no!
[Open] Write no!
[Open] Rdwr no!
As you can see, file permissions have changed and you cannot use the Open function to determine whether a file exists.
I believe many people like to use the open function to determine whether the file exists, of course, we all know about the issue of permissions, so we will use some necessary measures to control errors. But I recommend that you use an Access function to determine whether a file exists. When we want to read the contents of the file can use open to determine whether the file exists, even if it is because there is no Read permission or the file really does not exist, will not be able to achieve the read operation, our goal is to achieve