Board index » cppbuilder » testing for file existence, ios::noreplace does not work

testing for file existence, ios::noreplace does not work


2005-11-17 03:42:20 AM
cppbuilder81
I want to avoid overwriting an existing file so need to test whether a file exists.
I tried:
ofstream outputfile(outfilename,ios::noreplace);
if (!outputfile)
{
cout<<"Error File "<<outfilename<<" already exists";
return 1;
}
This gave me a compiler error:
[C++Error] SAMeas.cpp(119): Undefined symbol 'noreplace'.
What code can I use to check for file existence?
I'm using Borland C++ Builder 3.0
Jeff
 
 

Re:testing for file existence, ios::noreplace does not work

"Jeff" < XXXX@XXXXX.COM >writes:
Quote
I want to avoid overwriting an existing file so need to test whether a file exists.

I tried:
ofstream outputfile(outfilename,ios::noreplace);
if (!outputfile)
{
cout<<"Error File "<<outfilename<<" already exists";
return 1;
}

This gave me a compiler error:
[C++Error] SAMeas.cpp(119): Undefined symbol 'noreplace'.

What code can I use to check for file existence?
There is no such thing as ios::noreplace in standard C++. That was a
feature in pre-standard C++ that was dropped (for better or for
worse.) Even though it's Microsoft documention, this link is useful
for some iostream info: tinyurl.com/8g88h
Quote
I'm using Borland C++ Builder 3.0
I'd suggest you check for the file using stat or a windows API
function (if it's a>2GB file), and only open it if stat() indicates
the file does not already exist. (Well, that simulates the
ios::nocreate feature, but gives the general idea that "you need to
implement it yourself.")
--
Chris (TeamB);
 

Re:testing for file existence, ios::noreplace does not work

On 16 Nov 2005 12:42:20 -0700, Jeff wrote:
Quote
I want to avoid overwriting an existing file so need to test whether a file exists.
Try to open for read access. If that fails, then it doesn't exist,
and open for write.
--
liz
 

{smallsort}

Re:testing for file existence, ios::noreplace does not work

Tried out stat.
It seems to work.
Thanks!!!!
 

Re:testing for file existence, ios::noreplace does not work

Jeff wrote:
Quote
Tried out stat.
It seems to work.
You might also look at
int access(const char *filename, int amode);
if( access( filename, 0 ) != 0 )
Create_and_write_it();
 

Re:testing for file existence, ios::noreplace does not work

Jeff wrote:
Quote
I want to avoid overwriting an existing file so need to test whether a file exists.

I tried:
ofstream outputfile(outfilename,ios::noreplace);
if (!outputfile)
{
cout<<"Error File "<<outfilename<<" already exists";
return 1;
}
This is 100% portable way:
inline bool fileExists(const std::string& fname) {
return std::ifstream(fname.c_str()).is_open();
}
//Later in your code
if (!fileExists(myFile)) {
///do what you need to do
}
 

Re:testing for file existence, ios::noreplace does not work

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >writes:
Quote
I'd suggest you check for the file using stat or a windows API
function (if it's a>2GB file), and only open it if stat() indicates
the file does not already exist. (Well, that simulates the
ios::nocreate feature, but gives the general idea that "you need to
implement it yourself.")
But there is a race condition here. If some other thread or task
creates the file between the points in time when this thread calls
stat() and when it "creates" the file, the file may get overwritten.
I'm afraid something platform specific such as CreateFile() with
CREATE_NEW is needed for a clean solution.
 

Re:testing for file existence, ios::noreplace does not work

Wayne A. King wrote:
Quote
What happens if the file exists but is in use by another running program
and is locked against all use by other apps - including reading?
OK. You got a point there. In that case it is better to use either API
like GetFileAttributes, FindFirstFile or c function stat.
Darko
 

Re:testing for file existence, ios::noreplace does not work

Bob Gonder < XXXX@XXXXX.COM >wrote:
Quote
Jeff wrote:

>Tried out stat.
>It seems to work.

You might also look at

int access(const char *filename, int amode);

if( access( filename, 0 ) != 0 )
Create_and_write_it();

That's how I've been doing it for almost 20 years. ;-)