473,698 Members | 2,361 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
16 2226
Gary Wessle <ph****@yahoo.c omwrites:
Gary Wessle <ph****@yahoo.c omwrites:
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

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::stringstre am 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.comwr ites:
>>
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 );
}

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.c omwrites:
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
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.c omwrote,
std::fstream iofs( f.c_str(), std::ios::in|st d::ios::out );
if (!iofs) perror(f.c_str( ));
Mar 1 '07 #14
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.c omwrites:

>>Gary Wessle <ph****@yahoo.c omwrites:

>>>John Harrison <jo************ *@hotmail.comwr ites:
John Harrison wrote:

>typo
>
>
>>std::fstr eam 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

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::stringstre am 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.comwr ites:
>>>
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 );
}

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.comwr ote in message
news:AQ******** *******@newsfe3-win.ntli.net...
red floyd wrote:
>Gary Wessle wrote:
>>John Harrison <jo************ *@hotmail.comwr ites:
>>>>
std::fstre am 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 );
}
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
2373
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 places without any reason and regularity Can you advice something?
9
2187
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
3061
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
4692
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
3271
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 i686 athlon i386 GNU/Linux
6
62738
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
4193
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
2480
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 for how to avoid that errors
5
9092
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
8674
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.