Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a portable way of finding out if a file exists?
Posted

Good Old fopen() does the trick

C
FILE *fp;
if ((fp = fopen((LPCTSTR)fName, "r")) == NULL)
{
  // file doesn't exist
}
else
{
  fclose(fp);
  // file does exist
}


(Updated)

If you're going to downvote the answer, at least have the courtesy to say why. Does "fopen()" not work? Of course it does, on all platforms, C and C++. The other solution points to a link that outlines several ways but require 3rd party (boost) or use functions that have different names for Windows and Linux (_access(), access()) which isn't "code transparent", meaning that you have to conditionalize the code for platform. If you're going to do that, you might as well use the platform specific calls and give up the idea of platform agnostic calls.

I'm not saying this is the "ideal" or "only" answer, I'm saying it answered the OPs question as posed and is not a "bad answer", maybe it's just one you don't like, but not wrong.
 
Share this answer
 
v3
Comments
Albert Holguin 2-Nov-11 22:26pm    
Countered downvote... This works, no reason to downvote... 5
Chuck O'Toole 2-Nov-11 22:34pm    
Thanks
Sergey Alexandrovich Kryukov 3-Nov-11 0:24am    
I up-voted the answer by my 5, what to discuss it.

This solution is reasonable, and your criticism of some other solutions is quite correct. I would say though that the argument "this works, that's why this is good" is generally insufficient (to my taste, bad non-working solution is much better than working bad solution), but you just don't need this argument.

I can easily imagine the motivation of the down-voter, some kind of false purism, like a predicate should not have a side effect. So, formally, you did not answer the question. But who says the question is reasonable? Who said that the solution of the problem "find out if the file exists, non-intrusively" has to be found? Your approach is good just because it suggests using of offensive programming instead of defensive, without any problems.

Domination of defensive programming really contaminates programming. I often saw such an idiotic code: if (!FileExists(fileName)) throw new Exception("File not found"); which is idiotic because a simple attempt to open this file (offensive) will throw quite relevant exception anyway, without any problems.

--SA
Chuck O'Toole 3-Nov-11 0:46am    
Thanks SA. There are days when I do wear my "Master Software Engineer" hat (my actual job title) and defend architectural purity, especially when reviewing a new project proposal. However, I have a practical side which shows up when a reasonable answer is needed for an immediate problem. "Good because it works" is not a valid justification but "Rewrite it to be architecturally pure" is not a good response either. Especially when there is so little information about the application or the questioner's skills.

In this instance, the OP only wanted a portable way to check if a file existed. He didn't ask for a way without side effects (like actually opening the file). He didn't specify if he's using iostreams or standard IO or std:: io methods to manipulate the file. So, the simpliest, well known basic C functions that do the job was suggested. Now it's up to them to decide to use it or not or if the answer itself triggers some other idea for them.
Bala Selvanayagam 3-Nov-11 9:02am    
Chuck,

I personally prefer your solution, I truely do not know why the OP has accepted my solution and it could be that your solution was not posted (time delay) when the OP looked at / some other valid reason from OP's point of view which we do not know?

my5 for your solution
 
Share this answer
 
Comments
Albert Holguin 2-Nov-11 22:29pm    
Some of the suggestions listed in the link aren't that great... For example, if you're not using boost, no reason to use it just for this.
Bala Selvanayagam 3-Nov-11 8:59am    
Albert,

Your commments makes sense and I personally prefer Chuck's solution, I truely do not know why the OP has accepted my solution and it could be that Chuck's solution was not posted (time delay) when the OP looked at / some other valid reason from OP's point of view which we do not know?



I really do not know & hope this is a constructive discussion.

May be ask the OP ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900