473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

failed to open a stream


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
16 2198
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Barti | last post by:
After upgrading php from 4.2.3 to 4.3.4 I have problem with "failed to open stream" Warning: main(): Failed opening '...........inc.php' for inclusion...
9
by: One | last post by:
I have a main.php file that calls a php navigation menu. I want to pass the menu file a parameter to tell it which menu to display. Inside the main.php I have : include...
1
by: cainwebdesign | last post by:
php newbie needs help Warning: file(http://shop1.outpost.com/product/5053975) : failed to open stream: HTTP request failed! HTTP/1.1 404 Not Foundin I am just trying to put some code around...
1
by: kencana | last post by:
Hi all, I was wondering why I always get "failed to open stream: HTTP request failed!" error in either loading a normal or xml file. i don't understand why i can't get the whole result. the result...
5
by: xieliwei | last post by:
I have a freshly installed openSuSe 10.2 with PHP4 from http://download.opensuse.org/repositories/home:/michal-m:/php4/openSUSE_10.2/ (openSuSe abandoned PHP4 since version 10, but I have customers...
6
by: Andy2500 | last post by:
Hi, I'd like to upload an image to a folder, then I have 3 diffrents examples but all of them give an error "failed to open stream: Permission denied", althrough the C:\Inetpub\wwwroot is not...
1
by: anki21 | last post by:
Hello I am getting this error .Sometimes it works fine and sometimes not. : failed to open stream: HTTP request failed!
2
by: swethak | last post by:
Hi, when i run my code it gives error as fopen(lib/providers//provider.RVLogic.php): failed to open stream: Permission denied in F:\Facebook\furniture11\Data...
5
by: tyakimov | last post by:
Hi guys I got a problem On my IIS server I changed the password for the Internet Guest Account 'IUSR_Machine' and suddenly the PHP part of the intranet stopped working. - '... failed to open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.