What is the replacement for ios::noreplace in std?

D

Dennis

if you have the headers and main in below code snippet, the below ios::noreplace
creates an error:

error C2039: 'noreplace' : is not a member of 'basic_ios<char,struct
std::char_traits<char> >'
error C2065: 'noreplace' : undeclared identifier

The ios::noreplace allows the open to fail if the file already exists. What
does the new std library use to fail an open if the file already exists?

I need to use the std <fstream> rather than the older <fstream.h> because the
older version will not let me print a string with the statement
fdebug<<sMyString<<endl; where sMyString is defined in string sMyString;

Thanks for any help.

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
......
fstream fdebug("c:\\temp\\fdebug.txt", ios::noreplace);
.....
}
 
J

John Harrison

if you have the headers and main in below code snippet, the below
ios::noreplace
creates an error:

error C2039: 'noreplace' : is not a member of 'basic_ios<char,struct
std::char_traits<char> >'
error C2065: 'noreplace' : undeclared identifier

The ios::noreplace allows the open to fail if the file already exists.
What
does the new std library use to fail an open if the file already exists?

That's a pretty unusual requirement. I think your best bet is to open for
reading first, if that fails then you know the file doesn't exist.

fstream fdebug;
{
ifstream test("c:\\temp\\fdebug.txt");
if (!test.is_open())
{
// file doesn't exist so lets do the real open
fdebug.open(("c:\\temp\\fdebug.txt");
}
}

Untested code.

john
 
D

Dennis

John Harrison said:
That's a pretty unusual requirement. I think your best bet is to open for
reading first, if that fails then you know the file doesn't exist.

fstream fdebug;
{
ifstream test("c:\\temp\\fdebug.txt");
if (!test.is_open())
{
// file doesn't exist so lets do the real open
fdebug.open(("c:\\temp\\fdebug.txt");
}
}
Thanks John.

I had to use similar code:
ifstream fout(FileID);
bool file_exists=fout.good;
fout.close();
if(!file_exists){
ofstream fout(FileID, ios::eek:ut);
....
}

I had been using <fstream.h> and didn't have a problem until I started using
<string> class. when I attempted to write my string to a file with
fout<<sMyString<<endl; I got the error:

error C2679: binary '<<' : no operator defined which takes a right-hand operand
of type 'class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' (or there is no acceptable conversion)

Thus I had to switch to the std <fstream> which eliminated the ios::noreplace.

There's always something!
 
D

Dennis

John Harrison said:
That's a pretty unusual requirement. I think your best bet is to open for
reading first, if that fails then you know the file doesn't exist.

fstream fdebug;
{
ifstream test("c:\\temp\\fdebug.txt");
if (!test.is_open())
{
// file doesn't exist so lets do the real open
fdebug.open(("c:\\temp\\fdebug.txt");
}
}
John,

After working with this for quite a few days I've come to the conclusion that
there is no good substitute for ios::noreplace.

Since <fstream.h> will not allow fdebug<<sMyString<<endl; the easy way around
this is to use <fstream.h> with it's ios::noplace and to use:
for ( i=0; i<sMyString.size()-1; i++) {fdebug<<sMyString; } fdebug<<endl;
 
J

John Harrison

John,

After working with this for quite a few days I've come to the conclusion
that
there is no good substitute for ios::noreplace.

Why not? What was wrong with your solution (and mine)? After all I would be
willing to bet that the implementation of noreplace in fstream.h is just
more C++ code, so have a look in the header file at how they did it.
Since <fstream.h> will not allow fdebug<<sMyString<<endl; the easy way
around
this is to use <fstream.h> with it's ios::noplace and to use:
for ( i=0; i<sMyString.size()-1; i++) {fdebug<<sMyString; }
fdebug<<endl;


I think you mean 'for ( i=0; i<sMyString.size(); i++)'. Also you could
consider

fdebug<<sMyString.c_str();

which does the same thing provided sMyString does not contain a null
characters.

By using fstream.h and noreplace you are making your non-portable because
neither of these things are part of the standard C++ language.

john
 
D

Dennis

John Harrison said:
Why not? What was wrong with your solution (and mine)?

The reason the <fstream> replacement didn't work is complicated but I'll try and
explain. The calling program for my function where the <fstream> code is, puts
that function into a double loop. Each time it calls my function it's giving my
function a file to write to and the calculation results it wants to write in
that file. On the first pass for a given file the function opens a new file and
places a header as the first line in that file and the results on the next line.
On the next pass for a given file the function checks if that file exists with
the ios::noreplace and if the file exists it opens the file for append and
writes the next line. The calling program is writing to a 100+ files.

Now with <fstream> our method of opening the file for input, closing the file if
it exists and then opening the file for append just did not work. For some
reason the append did not work and the line after the header just keep getting
written over. So instead of 5000 lines plus header I ended up with just the
last calc line and a header. I tried many variations but all did not work. So
I went back to said:
After all I would be
willing to bet that the implementation of noreplace in fstream.h is just
more C++ code, so have a look in the header file at how they did it.
Hey this is a good idea I'll have to look at how the header implemented the
ios::noreplace.

As far as more code. My Program with <fstream.h> compiled into 108k. With the
<fstream> code my program compiled into 208k . Why the extra 100k? I don't
know. I'm using MS VC++ 6.0 with the latest SP.

I think you mean 'for ( i=0; i<sMyString.size(); i++)'. Also you could
consider Right my typo.

fdebug<<sMyString.c_str();

I tried this but <fstream.h> will not allow it.

Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top