The C language detects if the file exists int __cdecl access (const char *, int);

Source: Internet
Author: User
Tags win32

Recently write code, encountered many places need to determine whether the file exists. Online methods are also strange, "the schools of contention."

fopen Way open more see, there are other ways to determine whether the file exists, because other methods are not related to this article, so do not intend to mention.

I recently used WINAPI more, so by the way to search MSDN, found a function: pathfileexists

BOOL pathfileexists (  _in_ lpctstr pszpath);
The following is the author's initial approach, the Windows API in principle provides the function should be the most reasonable and efficient, at least this method on the Windows platform has enough bureaucratic flavor.
/*Minimum supported Client:windows Professional, Windows XP [desktop apps only]minimum supported Server:win Dows Server [Desktop Apps Only]dll:shlwapi.dll (version 4.71 or later)*/#include<Windows.h>//Windows API FindFirstFile#include <Shlwapi.h>//Windows API pathfileexists#pragmaComment (lib, "Shlwapi.lib")intMainvoid){    if(PathFileExists ("Setting.ini") {printf ("Load Customer setting...\n"); }Else{printf ("No setting file found,load default. \ n"); }return 0;}

A friend of the group mentioned fopen is a POSIX system open file FD standard Open, have doubts, if a file dozens of G, will not be in the process of fopen occupy a lot of resources,

Later verification received the reply:

I
'm sure it won't load.
As you said, what if the file Xxxg?

The file is accessed with pointers, so don't worry about your problems

Due to the author's current research scope and limited capacity, not to study.

The author's programs sometimes need to be compatible with Linux, usually compiled and tested under Cygwin, but also repeatedly feel that too dependent on the specific operating system of the API is a bit inappropriate.

Search on Google, StackOverflow gives an answer:

What's the best-by-check if a file exists in C? (Cross platform)

Using functions

int __cdecl access (constcharint);

The author commonly used tcc,visual C + + 6.0,GCC open tcc-win32-0.9.26 View related reference header file found Unistd.h is simply # include once IO.h

So bold guess, Visual C + + 6.0 header file directory may have io.h file, opened to look, sure enough, there are related function declarations.

According to StackOverflow method, the author wrote a Code of compatibility, the following code supports LINUX-GCC,TCC (Windows), Visual C++6.0,CYGWIN-GCC

#include <stdio.h>#ifDefined (WIN32) | | Defined (_WIN32) | | Defined (WIN64) | | Defined (_win64)#include<io.h>#ifndef F_OK#defineF_OK 0/* Check for file existence */#endif#endif#ifDefined (__cygwin__) | | Defined (__linux__) | | Defined (Linux) | | Defined (__linux)#include<unistd.h>#endifintMainvoid){    if(Access ("Setting.ini", F_OK)! =-1) {printf ("Load Customer setting...\n"); } Else{printf ("No setting file found,load default. \ n"); }}

At this point, my mother doesn't have to worry about it anymore. I'm not sure if the file exists;)

The C language detects if the file exists int __cdecl access (const char *, int);

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.