By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,317 Members | 2,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,317 IT Pros & Developers. It's quick & easy.

failed to open a stream

P: n/a

Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << '\n' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks
Feb 28 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Gary Wessle wrote:
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << '\n' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks
std::fstream iofs( f.c_str(), std::ios::out | std::ios::app );
if ( iofs )
{
iofs.close();
iofs.clear();
iofs.open( f.c_str(), std::ios::in|std::ios:out );
}

if ( !iofs )
{
// create or open failed.
}
Feb 28 '07 #2

P: n/a
Gary Wessle wrote:
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << '\n' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks

You really need to read the manual. I'd recommend this one

http://www.dinkumware.com/manuals/de..._filebuf::open

it very clear about what the different flags mean an what they do.

In your case std::ios::in|std::ios::out means open existing file for
reading and writing. It does not create a file, and it reports a failure
if the file does not exist. Which is exactly what you are seeing.

I can;t remember exactly what you are trying to do but I think you've
picked something that cannot be accomplished in one step. You must try
to open with one set of flags and then if that fails try a different
set. Something like

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john
Feb 28 '07 #3

P: n/a
typo
>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john
Feb 28 '07 #4

P: n/a
On Mar 1, 11:26 am, Gary Wessle <phd...@yahoo.comwrote:
>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << '\n' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a
failure?
Obviously the operating system was unable to create the file. To
find out exactly why , you should ask on an OS-specific newsgroup.

However, a reasonable guess is that your OS uses '/' as a path
separator, and there is no directory existing called "pair_status",
some operating systems do not automatically create directories
when you try to createa file.

Another possibility might be that you do not have permission to
create such a file, or perhaps the current directory is not what
you think it is.

Feb 28 '07 #5

P: n/a
Old Wolf wrote:
On Mar 1, 11:26 am, Gary Wessle <phd...@yahoo.comwrote:
> std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << '\n' << iofs.is_open() << std::endl;

puts out
************************************************ ****************
pair_status/myPair
0
************************************************ ****************

and the file is not created, why is_open() is reporting a
failure?


Obviously the operating system was unable to create the file. To
find out exactly why , you should ask on an OS-specific newsgroup.

However, a reasonable guess is that your OS uses '/' as a path
separator, and there is no directory existing called "pair_status",
some operating systems do not automatically create directories
when you try to createa file.

Another possibility might be that you do not have permission to
create such a file, or perhaps the current directory is not what
you think it is.
The combination of flags the OP was using never creates a file.

john
Feb 28 '07 #6

P: n/a
John Harrison wrote:
typo
>>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())


if (!iofs.is_open())
>{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john
No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john
Feb 28 '07 #7

P: n/a
John Harrison <jo*************@hotmail.comwrites:
John Harrison wrote:
typo
>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john

No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john
thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks
Feb 28 '07 #8

P: n/a
Gary Wessle wrote:
John Harrison <jo*************@hotmail.comwrites:

>>John Harrison wrote:
>>>typo
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())

if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john

No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john


thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks
Nothing I can think of immediately. Could you post the complete section
of code, from when you opened the file to when the write appeared to fail.

You could also try this

if (!(iofs << "reversals" << " " << val << std::endl))
{
std::cerr << "write failed!!\n";
}

to see if C++ thinks the write failed or not.

john
Mar 1 '07 #9

P: n/a
John Harrison wrote:
Gary Wessle wrote:
>John Harrison <jo*************@hotmail.comwrites:

>>John Harrison wrote:

typo
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}
>
john
No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john

thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have
iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks


Nothing I can think of immediately. Could you post the complete section
of code, from when you opened the file to when the write appeared to fail.

You could also try this

if (!(iofs << "reversals" << " " << val << std::endl))
{
std::cerr << "write failed!!\n";
}

to see if C++ thinks the write failed or not.

john
Actually I can think of two things.

The two classic mistakes when trying to read and write, are to read
until you hit the end of file and then think the stream is still OK (for
writing or whatever), or to think that you can just switch between
reading and writing without doing the necessary magic.

If either of those sound like what you're trying to do let me know.

john
Mar 1 '07 #10

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
Gary Wessle <ph****@yahoo.comwrites:
John Harrison <jo*************@hotmail.comwrites:
John Harrison wrote:
typo

>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
if (!iofs.is_open())

{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}
>
john
>
No I got that very wrong (maybe I should read th emanual) here's a
better attempt.
>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}
>
That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.
>
john
thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks

the file which as created has the right permissions.
-rw-r--r-- 1 fred fred 0 Mar 1 10:52 myPair
ok, very strange as it is but I don't know how to fix it.

after the file is created with the above setup, I have

while( !found_it && getline( iofs, line ) )
{
std::stringstream ss( line );
ss >word;
if( word == lookup )
{
found_it = true;
ss >val;
m_pair_status[lookup] = val;
iofs.close();
return val;
}
}

iofs << "reversals" << " " << val << std::endl;

the while block does not get executed in this case, but it does
something to the stream that makes the line after the while block fail
to print any thing in the open stream.

hummm
Mar 1 '07 #11

P: n/a
Gary Wessle wrote:
John Harrison <jo*************@hotmail.comwrites:
>>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks
I think you need a iofs.clear() inside the braces, before the second
open call.

Mar 1 '07 #12

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
John Harrison <jo*************@hotmail.comwrites:
John Harrison wrote:
typo
>
>>
>std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
>if (iofs.is_open())
if (!iofs.is_open())
>
>{
> iofs.open( f.c_str(), ios_base::in | ios_base::out |
> ios_base::app );
>}
>>
>john
No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john

thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks
the file which as created has the right permissions.
-rw-r--r-- 1 fred fred 0 Mar 1 10:52 myPair
Mar 1 '07 #13

P: n/a
On 01 Mar 2007 09:26:47 +1100 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs) perror(f.c_str());
Mar 1 '07 #14

P: n/a
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.comwrites:

>>Gary Wessle <ph****@yahoo.comwrites:

>>>John Harrison <jo*************@hotmail.comwrites:
John Harrison wrote:

>typo
>
>
>>std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
>>if (iofs.is_open())
>
>if (!iofs.is_open())
>
>
>>{
> iofs.open( f.c_str(), ios_base::in | ios_base::out |
> ios_base::app );
>>}
>>
>>john

No I got that very wrong (maybe I should read th emanual) here's a
better attempt.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

That will open an existing file for reading and writing, but if it
doesn't exist it will create a new file for reading and writing.

john

thank you.

that did create the file, and stepping through with gdb shows that
the if block is opening the file, but few lines down the code I have

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks

the file which as created has the right permissions.
-rw-r--r-- 1 fred fred 0 Mar 1 10:52 myPair


ok, very strange as it is but I don't know how to fix it.

after the file is created with the above setup, I have

while( !found_it && getline( iofs, line ) )
{
std::stringstream ss( line );
ss >word;
if( word == lookup )
{
found_it = true;
ss >val;
m_pair_status[lookup] = val;
iofs.close();
return val;
}
}

iofs << "reversals" << " " << val << std::endl;

the while block does not get executed in this case, but it does
something to the stream that makes the line after the while block fail
to print any thing in the open stream.

hummm
It's not very strange, it's how it supposed to work.

When you cause an error on a stream (such as reading past the end of
while) the stream is in a error state. When a stream is in an error
state nothing further will work until you clear that error state. Add
the following code.

while( !found_it && getline( iofs, line ) )
{
...
}
iofs.clear(); // clear the error state
iofs << "reversals" << " " << val << std::endl;

john
Mar 1 '07 #15

P: n/a
red floyd wrote:
Gary Wessle wrote:
>John Harrison <jo*************@hotmail.comwrites:
>>>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}

iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks

I think you need a iofs.clear() inside the braces, before the second
open call.
Yes you're right. I think this is a case where the standard said
something different to what was intended. But some library writers took
them at their word and so iofs.clear() is necessary before the second
attempt to open.

john
Mar 1 '07 #16

P: n/a
"John Harrison" <jo*************@hotmail.comwrote in message
news:AQ***************@newsfe3-win.ntli.net...
red floyd wrote:
>Gary Wessle wrote:
>>John Harrison <jo*************@hotmail.comwrites:
>>>>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::trunc );
}
iofs << "reversals" << " " << val << std::endl;

that is not printing out any thing to the file that just been created.

whats wrong with this, I am using linux FC5.

thanks

I think you need a iofs.clear() inside the braces, before the second open
call.

Yes you're right. I think this is a case where the standard said something
different to what was intended. But some library writers took them at
their word and so iofs.clear() is necessary before the second attempt to
open.
Nope. The C++ Standard captures the behavior of the old, original
iostreams package that accompanies cfront. *All* of us library writers
took them at their word and did the silly thing, since we had no
wiggle room to do the sensible thing instead. IIRC, there's a DR that
might still fix this for the next revision of the C++ Standard.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Mar 1 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.