delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/05/16:42:55

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: <Pine DOT SUN DOT 3 DOT 91 DOT 1000105111921 DOT 7920d-100000 AT is>
Message-ID: <slrn8779f6.3vvou4b.mad@asylum.fasttrack.net>
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 <eliz AT is DOT elta DOT co DOT il> 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 <errno.h>
    #include <unistd.h>
    #include <sys/stat.h>

    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <string>

    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.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019