fstream vs ofstream

C

chiku

Hi,

When i tried below code it seems i am getting error in open method but
same program when i tried with ofstream object insted of fstream i got
success . Their was NO already existing file with passed name . Any
idea why?

Chiku

#include<iostream>
#include<fstream>

using namespace std;
int main(void)
{

fstream mystream;
mystream.open("testdirect.txt");
if(mystream.is_open())
{
cout << "file open success"<<"\n";
}
else
{
cout << "failed"<<"\n";
}

return 0;

}
 
J

Jim Langston

chiku said:
Hi,

When i tried below code it seems i am getting error in open method but
same program when i tried with ofstream object insted of fstream i got
success . Their was NO already existing file with passed name . Any
idea why?

Chiku

#include<iostream>
#include<fstream>

using namespace std;
int main(void)
{

fstream mystream;
mystream.open("testdirect.txt");
if(mystream.is_open())
{
cout << "file open success"<<"\n";
}
else
{
cout << "failed"<<"\n";
}

return 0;

fstream is an input/output stream. The constructor (from what I"m seeing)
takes a 2nd paramater specifying how to open it. I.E. app, ate, binary,
in, out, trunc. An example showing usage is:

// using fstream constructors.
#include <iostream>
#include <fstream>
using namespace std;

int main () {

fstream filestr ("test.txt", fstream::in | fstream::eek:ut);

// >> i/o operations here <<

filestr.close();

return 0;
}

Notice: your code is using .open This code is using the constructor. Your
code does not specify if the file exists or not, how to open it, etc.. this
code does.

I've never had much luck using .open myself and have always used the
constructor.
 
V

Vladimir Jovic

Jim said:
fstream is an input/output stream. The constructor (from what I"m
seeing) takes a 2nd paramater specifying how to open it. I.E. app,
ate, binary, in, out, trunc. An example showing usage is:

// using fstream constructors.
#include <iostream>
#include <fstream>
using namespace std;

int main () {

fstream filestr ("test.txt", fstream::in | fstream::eek:ut);

// >> i/o operations here <<

filestr.close();

return 0;
}

Notice: your code is using .open This code is using the constructor.
Your code does not specify if the file exists or not, how to open it,
etc.. this code does.

I've never had much luck using .open myself and have always used the
constructor.

It is the same, except you can use exceptions with open (not sure if it
is possible with constructor), like this:

#include <iostream>
#include <fstream>
using namespace std;

int main () {

fstream filestr;
filestr.exception( fstream::eofbit |
fstream::failbit |
fstream::badbit );
try
{
filestr.open("test.txt", fstream::in | fstream::eek:ut);
}
catch ( fstream::failure &e)
{
cout << "Exception opening/reading file";
}


// >> i/o operations here <<

filestr.close();

return 0;
}
 
V

Vladimir Jovic

Forgot to answer the question. The reason can be anything - file
missing, not enough access, opening read only file for writing, etc.
 
J

James Kanze

When i tried below code it seems i am getting error in open
method but same program when i tried with ofstream object
insted of fstream i got success . Their was NO already
existing file with passed name . Any idea why?

The mode parameter of ofstream defaults to ios::eek:ut. The mode
parameter of fstream defaults to ios::eek:ut | ios::in. Just
ios::eek:ut says to create a new file, or truncate an existing one.
ios::eek:ut | ios::in says to open an existing file, without
modifying its contents.

The open modes are defined by mapping them to the mode arguments
of fopen, so it's not that clean.
 
J

James Kanze

fstream is an input/output stream. The constructor (from what
I"m seeing) takes a 2nd paramater specifying how to open it.

Both the constructor and the open function have default
arguments for the second parameter.
I.E. app, ate, binary, in, out, trunc. An example showing
usage is:
// using fstream constructors.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
fstream filestr ("test.txt", fstream::in | fstream::eek:ut);

Those are the defaults. This changes nothing with respect to
what he has done.
// >> i/o operations here <<
filestr.close();
return 0;
}
Notice: your code is using .open This code is using the
constructor. Your code does not specify if the file exists or
not, how to open it, etc.. this code does.
I've never had much luck using .open myself and have always
used the constructor.

What's the difference? All the constructor does (in addition to
what the default constructor does) is call open.
 
P

Paul Carter

What's the difference?  All the constructor does (in addition to
what the default constructor does) is call open.

He may have been trying to reuse an iostream by reopening it using
..open and not resetting the flags. I've seen several people run
into problems where they have read to EOF, then try to
reuse the stream object using .open and being surprised that the
object state wasn't reset.
 

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

Similar Threads

fstream File i/o 1
fstream Buffers 26
iostream conflicts fstream? 6
fstream and ofstream 2
Chatbot 0
Filter sober in c++ don't pass test 0
Question about fstream operator >> and << 5
fstream - write a file 3

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top