473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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);
.....
}

Jul 22 '05 #1
5 6373

<De****@NoSpam.com> wrote in message
news:c5********************************@4ax.com...
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
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote:

<De****@NoSpam.com> wrote in message
news:c5********************************@4ax.com.. .
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");
}
}

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::out);
....
}

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!

Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote:

<De****@NoSpam.com> wrote in message
news:c5********************************@4ax.com.. .
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");
}
}

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[i]; } fdebug<<endl;

Jul 22 '05 #4

<De****@NoSpam.com> wrote in message
news:92********************************@4ax.com...
"John Harrison" <jo*************@hotmail.com> wrote:

<De****@NoSpam.com> wrote in message
news:c5********************************@4ax.com. ..
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");
}
}

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[i]; }
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
Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> wrote:
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)?


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 <fstream.h>
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Magnusson | last post by:
Hi, when compiling the following code with gcc or g++, I get the error message "test.cpp:6: `noreplace' is not a member of type `std::basic_ios<char, std::char_traits<char> >'" Shouldn't this...
6
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with...
6
by: Rudolfs.Bundulis | last post by:
As I understood it's better to use fstream instead of fstream.h. When I was porting my code (i'm using VC6.0++) i needed to let go of such things as ios::noreplace and ios::nocreate. I neede to...
1
by: Ria | last post by:
How to copy the contents of a file into another file in C++. how to use these parameters in c++ code (i) ios::in (ii) ios::out (iii) ios::noreplace
3
sangeetha jagannathan
by: sangeetha jagannathan | last post by:
hi can anyone explain me this code, instruction by instruction. i am not able to follow it. #include <iostream.h> #include <fstream.h> #include <stdio.h> #include "logger.h" #include...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.