From: mad AT intersurf DOT com (Mike Demoulin) Newsgroups: comp.os.msdos.djgpp Subject: Re: What is the canonical way to find out if a file exists (in g++)? References: Message-ID: User-Agent: slrn/0.9.5.7 (Windows) Lines: 72 Date: Wed, 05 Jan 2000 20:03:54 GMT NNTP-Posting-Host: 216.115.146.142 X-Complaints-To: news AT intersurf DOT net X-Trace: dim.intersurf.net 947102634 216.115.146.142 (Wed, 05 Jan 2000 14:03:54 CST) NNTP-Posting-Date: Wed, 05 Jan 2000 14:03:54 CST Organization: Intersurf Online, Inc. To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Wed, 5 Jan 2000 11:19:40 +0200 (IST), Eli Zaretskii wrote: > >On Tue, 4 Jan 2000, Mike Demoulin wrote: > >> In g++, I have used stat (), __file_exists (), and ifstream ().good (). >> All of these can return false negatives under NT. What am I doing wrong? > >I have never heard about `stat' and `__file_exists' returning false >negatives, in any environment. Please post the details (a short >program and the directory listing by DIR would be nice). Here is the directory listing: C:\FT>dir C:\FT\FT4WIN\FTCOMINTEGRATED.OCX.ZIP Volume in drive C has no label. Volume Serial Number is F4DE-6D7B Directory of C:\FT\FT4WIN 12/28/99 07:15p 58,134 FTCOMINTEGRATED.OCX.ZIP 1 File(s) 58,134 bytes 326,311,936 bytes free Here is the test program: #include #include #include #include #include #include #include using namespace std; void TestFileExists (const string &file) { cout << "Testing for \"" << file << "\"" << endl; struct stat buff; if (__file_exists (file.c_str ())) cout << "__file_exists () found the file" << endl; else cout << "__file_exists () found the file" << endl; if (!stat (file.c_str (), &buff)) cout << "stat () found the file" << endl; else perror ("stat () did not find the file"); if (ifstream (file.c_str (), ios::nocreate).good ()) cout << "ifstream found the file" << endl; else cout << "ifstream did not found the file" << endl; } int main (int, char *argv []) { TestFileExists (argv [1]); return 0; } Here are the results: Testing for "C:\FT\FT4WIN\FTCOMINTEGRATED.OCX.ZIP" __file_exists () found the file stat () did not find the file: No such file or directory (ENOENT) ifstream did not found the file This time, __file_exists worked, but there have been times that it didn't. >What version of DJGPP are you using, btw? djdev202. A few more details. I develope on a W95 machine, but this program is sent to users who don't have DJGPP installed. The program run above is on an NT machine, using the administrator account. Thanks for the help.