delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/25/18:01:49

From: Campbell Allan <allanca AT dcs DOT gla DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Checking to see if I file exists
Date: Thu, 23 Oct 1997 11:48:36 +0100
Organization: Department of Computing Science - Glasgow University
Lines: 30
Message-ID: <344F2B84.7979@dcs.gla.ac.uk>
References: <199710210043 DOT RAA15253 AT adit DOT ap DOT net>
NNTP-Posting-Host: bleaker.dcs.gla.ac.uk
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Nate Eldredge wrote:
> 
> At 02:18  10/20/1997 -0400, H. Anthony Hoyt wrote:
> >     I can't seem to remember how to check to see if I file exists or not.
> >What I want is, if the user enters in a file name, to check to see if the
> >file exists.  If so, I want to open the file, not overwrite it, read from
> >the file then recreate the file before I finaly save the file.  (All in
> >binary)  Any advice would be welcome.  Thanks
> Try the very confusingly :) named function __file_exists(). If you want to
> be portable, use access() or stat().
> 

What's wrong with trying to open the file as normal? I use fopen which
returns a pointer to FILE. If the file doesn't exist or can't be
read/written to for some reason then a NULL is returned instead of the
pointer. ie.

	FILE *fp;

	fp=fopen("myfile.dat","br");
	if (fp==NULL)
	  {
	     printf("Error datafile does not exist!\n");
	     exit(EXIT_FAILURE);
          }	

You should always do this when reading files to make sure your program
doesn't crash out without telling the user why. 

Campbell

- Raw text -


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