Programming question

Ben Pfaff pfaffben@msu.edu
21 Jan 2001 04:13:39 -0500


Edward Glowacki <glowack2@msu.edu> writes:

> On Fri, 19 Jan 2001, Don Chorman wrote:
> > I am writing a program in C++, and I need it to write to
> > a new file. I'm know how to do this part, but I want to 
> > avoid overwriting files, or atleast warn the user that a 
> > particular file exists. Can I code this in C++?
> 
> The quick and relatively useless answer is: Yes, and Yes.
> 
> There are probably a dozen different ways to check to see if a file
> exists.  "stat()" is one such C call you can use, there might be 
> better ones for C, and for C++ there may be some others, but at
> least its a place to start.

stat() is a poor way to do this, because there is a race
condition:

	Your Program			Another Program
	------------			---------------
	stat() - is file `foo' here?
	no, there isn't...
					create file `foo'
	create (overwrite) file `foo'
-- 
"Let others praise ancient times; I am glad I was born in these."
--Ovid (43 BC-18 AD)