bug in fstream? (works with fstream.h) 
Author Message
 bug in fstream? (works with fstream.h)

What are the differences between fstream and fstream.h???

Consider the following simple program

#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
    fstream file("abc.dat", ios::in | ios::out |
ios::binary);
    if (!file.is_open())
    {
       cout << "Error " << endl;
       return 1;
    }
    return 0;

Quote:
}

If I use fstream.h (and iostream.h), it works fine,
creates the file.  If I use fstream and iostream, compiles
fine but gives the error message and cannot create the
file. What is the problem???  Thanks for any help!


Sat, 27 Aug 2005 03:32:38 GMT  
 bug in fstream? (works with fstream.h)
In standard iostreams, (in | out | binary) flags correspond to "r+b"
mode of fopen(). Under C standard, fopen with this mode should fail if
the file does not exist, which is what happens in your program. If you
want to create new file or truncate existing file, you need "w+b" mode
which corresponds to (in | out | trunc | binary). There is no mode that
allows to open an existing file, or create a new one if the file does
not exist. For this, you can write something like:

fstream file;
// Open existing
if (!file.open("abc.dat", ios::in | ios::out | ios::binary))
{
    // Create new
    file.open("abc.dat", ios::in | ios::out | ios::trunc | ios::binary);

Quote:
}

if (!file.is_open())
{
    // Report error

Quote:
}

Probably the behavior of pre-standard iostreams was different.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> What are the differences between fstream and fstream.h???

> Consider the following simple program

> #include <iostream>
> #include <fstream>
> using namespace std;
> int main ()
> {
>     fstream file("abc.dat", ios::in | ios::out |
> ios::binary);
>     if (!file.is_open())
>     {
>        cout << "Error " << endl;
>        return 1;
>     }
>     return 0;
> }

> If I use fstream.h (and iostream.h), it works fine,
> creates the file.  If I use fstream and iostream, compiles
> fine but gives the error message and cannot create the
> file. What is the problem???  Thanks for any help!



Sat, 27 Aug 2005 04:31:07 GMT  
 bug in fstream? (works with fstream.h)


Quote:
> What are the differences between fstream and fstream.h???

I don't know.  I just try to use the newer,
standard-defined classes as documented.

Quote:
> Consider the following simple program

> #include <iostream>
> #include <fstream>
> using namespace std;
> int main ()
> {
>     fstream file("abc.dat", ios::in | ios::out | ios::binary);

How should a fstream opened for read behave
when the file does not exist?

Quote:
>     if (!file.is_open())

This succeeds if either { the file exists }
or {  ios::trunc is added to the open flags }.

...

Quote:
> If I use fstream.h (and iostream.h), it works fine,
> creates the file.  If I use fstream and iostream, compiles
> fine but gives the error message and cannot create the
> file. What is the problem???  Thanks for any help!

I think the problem will become evident via
study of the meanings of the open flags.  A
number of changes were made in this area, so
a review is in order anyway.

--
-Larry Brasfield
(address munged, s/sn/h/ to reply)



Sat, 27 Aug 2005 04:43:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. fstream ./. fstream.h

2. Serious BUG in fstream

3. fstream bug?

4. Bug in iostream / fstream

5. Bug in MS's fstream?

6. Bug in fstream ?

7. fstream: I can't get it to work correctly

8. fstream in dll does not work

9. fstream not working in VC

10. fstream not working in VC6

11. pb with replacing <fstream.h> by <fstream>

12. <fstream.h> and <fstream> chaos

 

 
Powered by phpBB® Forum Software