failed to open a stream

G

Gary Wessle

Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::eek:ut );
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
 
R

red floyd

Gary said:
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::eek:ut );
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::eek:ut | std::ios::app );
if ( iofs )
{
iofs.close();
iofs.clear();
iofs.open( f.c_str(), std::ios::in|std::ios:eek:ut );
}

if ( !iofs )
{
// create or open failed.
}
 
J

John Harrison

Gary said:
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::eek:ut );
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/default.aspx?manual=compleat&page=fstream.html#basic_filebuf::open

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

In your case std::ios::in|std::ios::eek:ut 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::eek:ut );
if (iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::eek:ut |
ios_base::app );
}

john
 
O

Old Wolf

std::fstream iofs( f.c_str(), std::ios::in|std::ios::eek:ut );
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.
 
J

John Harrison

Old said:
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
 
J

John Harrison

John said:
typo



if (!iofs.is_open())

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::eek:ut );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::eek:ut |
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
 
G

Gary Wessle

John Harrison said:
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::eek:ut );
if (!iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::eek:ut |
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
 
J

John Harrison

Gary said:
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
 
G

Gary Wessle

Gary Wessle said:
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
 
J

John Harrison

John said:
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
 
G

Gary Wessle

Gary Wessle said:
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
 
R

red floyd

Gary said:
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.
 
J

John Harrison

Gary said:
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
 
J

John Harrison

red said:
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
 
P

P.J. Plauger

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
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top