Board index » cppbuilder » testing for file existence, ios::noreplace does not work
Jeff
![]() CBuilder Developer |
Jeff
![]() CBuilder Developer |
testing for file existence, ios::noreplace does not work2005-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 |
Chris Uzdavinis
![]() CBuilder Developer |
2005-11-17 06:15:37 AM
Re:testing for file existence, ios::noreplace does not work
"Jeff" < XXXX@XXXXX.COM >writes:
QuoteI want to avoid overwriting an existing file so need to test whether a file exists. worse.) Even though it's Microsoft documention, this link is useful for some iostream info: tinyurl.com/8g88h QuoteI'm using Borland C++ Builder 3.0 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); |
liz
![]() CBuilder Developer |
2005-11-17 06:18:29 AM
Re:testing for file existence, ios::noreplace does not work
On 16 Nov 2005 12:42:20 -0700, Jeff wrote:
QuoteI want to avoid overwriting an existing file so need to test whether a file exists. -- liz {smallsort} |
Jeff
![]() CBuilder Developer |
2005-11-17 06:57:00 AM
Re:testing for file existence, ios::noreplace does not work
Tried out stat.
It seems to work. Thanks!!!! |
Bob Gonder
![]() CBuilder Developer |
2005-11-17 11:14:28 PM
Re:testing for file existence, ios::noreplace does not work
Jeff wrote:
QuoteTried out stat. if( access( filename, 0 ) != 0 ) Create_and_write_it(); |
Darko Miletic
![]() CBuilder Developer |
2005-11-18 12:05:54 AM
Re:testing for file existence, ios::noreplace does not work
Jeff wrote:
QuoteI want to avoid overwriting an existing file so need to test whether a file exists. return std::ifstream(fname.c_str()).is_open(); } //Later in your code if (!fileExists(myFile)) { ///do what you need to do } |
maeder
![]() CBuilder Developer |
2005-11-18 03:02:05 AM
Re:testing for file existence, ios::noreplace does not work
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >writes:
QuoteI'd suggest you check for the file using stat or a windows API 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. |
Darko Miletic
![]() CBuilder Developer |
2005-11-18 08:08:13 PM
Re:testing for file existence, ios::noreplace does not work
Wayne A. King wrote:
QuoteWhat happens if the file exists but is in use by another running program Darko |
Matt Jacobs
![]() CBuilder Developer |
2005-11-23 11:32:37 AM
Re:testing for file existence, ios::noreplace does not work
Bob Gonder < XXXX@XXXXX.COM >wrote:
QuoteJeff wrote: |