Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does access function check the existence of file?

Tags:

c

file

The access function checks to see whether the file can be accessed in the way specified by the how argument. The how argument either can be the bitwise OR of the flags R_OK, W_OK, X_OK, or the existence test F_OK.

The return value is 0 if the access is permitted, and -1 otherwise.

if the file does not exist, Does the access return -1 too?

I wan to develop a function in which I check the existence of a file. If the following access function did it, what kind of arguments I have to put according to the standard?

if (access("file_example", R_OK | W_OK | X_OK) != -1)

BTW: the file I want to check if exisits by access() function is created by the same application. so it's created by the same user

like image 216
MOHAMED Avatar asked Nov 22 '12 08:11

MOHAMED


People also ask

How do you check if a file exists in MS Access VBA?

Use the VBA Dir function to check if a file exists. The VBA Dir function returns the name of a valid file, so you can use it to test whether a file exists. When the VBA Dir function returns an empty string, it means the file does not exist.

Which function is used to check whether the file exists or not?

The file_exists() function checks whether a file or directory exists. Note: The result of this function is cached. Use clearstatcache() to clear the cache.

How check if file exists folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists.

How do you check if a file exists in Excel?

You can run the macro by select Macro > Macros under the Tools menu. Then select the macro called CheckIfFileExists and click on the Run button. After you run the macro, the values in column B should indicate whether the files exist or not. You can press Alt + F11 to view the VBA code.


4 Answers

Those flags would check if the file is executable, writeable and readable by the process, lots of files won't be. The flag you're looking for is F_OK. F_OK tests for the existence of the file and nothing else.

I suggest reading the man page for access. It should be documented there.

like image 182
Art Avatar answered Nov 14 '22 07:11

Art


You can use it.

if (access("file_example", F_OK) != -1)

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

The value of amode is either the bitwise-inclusive OR of the access permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).

like image 25
jouyouyun Avatar answered Nov 14 '22 07:11

jouyouyun


Yes, with errno set to ENOENT.

like image 42
Some programmer dude Avatar answered Nov 14 '22 06:11

Some programmer dude


According to the manual page, one of the errors returned is:

 ENOENT A component of pathname does not exist or is a dangling symbolic link.

Also, in the second paragraph it clearly says:

 F_OK tests for the existence of the file.
like image 37
Code Painters Avatar answered Nov 14 '22 05:11

Code Painters