Does the open operation succeed?

F

fl

Hi,

Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).

Could you tell me how to check "in" stream?
Thanks,


................
#include "io_preamble.h"

// opens in binding it to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors

// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given

return in; // condition state is good if open succeeded
}
 
V

Victor Bazarov

Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).

Could you tell me how to check "in" stream?

try 'in.good()'.
Thanks,


...............
#include "io_preamble.h"

// opens in binding it to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors

// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given

return in; // condition state is good if open succeeded
}

V
 
B

Bill Gill

Hi,

Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).

Could you tell me how to check "in" stream?
Thanks,


...............
#include "io_preamble.h"

// opens in binding it to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors

// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given

return in; // condition state is good if open succeeded
}
It looks to me as though you need to check the value returned
by open_file to see if the open worked.

if(!openfile(in, filename))
.... // recovery code

Bill Gill
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top