473,564 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

failed to open a stream


Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|st d::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 2209
Gary Wessle wrote:
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|st d::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|st d::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|st d::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|st d::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|st d::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|st d::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.c omwrote:
>
std::fstream iofs( f.c_str(), std::ios::in|st d::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_statu s",
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.c omwrote:
> std::fstream iofs( f.c_str(), std::ios::in|st d::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_statu s",
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|st d::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|st d::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.comwr ites:
John Harrison wrote:
typo
>
std::fstream iofs( f.c_str(), std::ios::in|st d::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|st d::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.comwr ites:

>>John Harrison wrote:
>>>typo
std::fstrea m iofs( f.c_str(), std::ios::in|st d::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::fstrea m iofs( f.c_str(), std::ios::in|st d::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.comwr ites:

>>John Harrison wrote:

typo
std::fstrea m iofs( f.c_str(), std::ios::in|st d::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::fstrea m iofs( f.c_str(), std::ios::in|st d::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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2369
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 (include_path='/usr/local/lib:/usr/local/lib/php') in /home/file.php on line 2 When I refersh browser everything is ok. Mentioned file exist and has correct rights. This happens in random...
9
2179
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 "/home/ottadca1/public_html/includes/leftnav.php?menuid=dave"; But the error is : Warning:
1
3055
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 this to trap it.
1
4679
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 i get is only some part of the respective url (the one i tried to load). The following is my php code to open the file: $file=...
5
3260
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 who need php4 support) The version strings are as follows: # uname -a Linux server2 2.6.18.2-34-default #1 SMP Mon Nov 27 11:46:27 UTC 2006...
6
62691
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 protected. Any suggestions would be helpful, thank you for your time.
1
4179
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
2474
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 Mining\public_html\lib\inc\functions.lib.php Line 673 : trigger_error("Failed to open stream to $filename. Permission denied!", E_USER_ERROR); could you plz tell the solution...
5
9086
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 stream: HTTP request failed! HTTP/1.1 401 Access Denied in ...' My PHP script is very simple: <?php
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.