473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Irregular behaviour: C++ standard lib and file stream.

Hello,

I suspect a platform bug, but first I'd like to check whether the C++
standard library has changed the handling of streams in a way that creates
the
behaviour I'm observing. Hence this posting.

The following code fails when it's compiled with standard library, but
works without, refer to the commented sections. When failing, only the
first file open succeeds, subsequent ones generate an error. Is this
explainable 'within the rules?'

If anyone would like more information on specific platform details, email
me.

bx******@weqstn et.com.au
remove ecks and kyoo to email.

// works with this active
// *************** *************** *************
#include <iostream.h>
#include <fstream.h>
// *************** *************** *************

// Fails with this active
// *************** *************** *************
#include <iostream>
#include <fstream>
using namespace std ;
// *************** *************** *************

int main()
{
ifstream ifs ;
int i ;
char c ;
for (i = 0; i < 5; i++)
{
ifs .open ("TEST.TXT") ;
if (!ifs)
{
cout << "\nERROR" ;
return 1 ;
}
while (ifs) ifs.get (c) ;
ifs.close() ; // Strictly unnecessary, since stream has terminated.
// Removing makes no difference.
}
return 0 ;
}


Jul 22 '05 #1
2 1455
On Tue, 25 Nov 2003 21:50:01 +0800, "bruce varley"
<bx******@weqst net.com.au> wrote:
Hello,

I suspect a platform bug, but first I'd like to check whether the C++
standard library has changed the handling of streams in a way that creates
the
behaviour I'm observing. Hence this posting.

The following code fails when it's compiled with standard library, but
works without, refer to the commented sections. When failing, only the
first file open succeeds, subsequent ones generate an error. Is this
explainable 'within the rules?'
Yup, when a stream hits EOF it stays that way until you "clear" the
error state. See below.
#include <iostream>
#include <fstream>
using namespace std ;

int main()
{
ifstream ifs ;
int i ;
char c ;
for (i = 0; i < 5; i++)
{
ifs .open ("TEST.TXT") ;
if (!ifs)
{
cout << "\nERROR" ;
return 1 ;
}
while (ifs) ifs.get (c) ;


Add here:
ifs.clear(); //clears error state

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2

"bruce varley" <bx******@weqst net.com.au> wrote in message
news:3f******@q uokka.wn.com.au ...
Hello,

I suspect a platform bug, but first I'd like to check whether the C++
standard library has changed the handling of streams in a way that creates
the
behaviour I'm observing. Hence this posting.

The following code fails
Please define what you mean by 'fails'. Does it not compile?
Does it give unexpected behavior, and if so, what?
when it's compiled with standard library, but
works without, refer to the commented sections. When failing, only the
first file open succeeds, subsequent ones generate an error. Is this
explainable 'within the rules?'

If anyone would like more information on specific platform details, email
me.

bx******@weqstn et.com.au
remove ecks and kyoo to email.

// works with this active
// *************** *************** *************
#include <iostream.h>
#include <fstream.h>
Neither of these headers are, or ever have been, part of
standard C++, so I'll not comment further upon them.
// *************** *************** *************

// Fails with this active
// *************** *************** *************
#include <iostream>
#include <fstream>
using namespace std ;
// *************** *************** *************

int main()
{
ifstream ifs ;
int i ;
char c ;
for (i = 0; i < 5; i++)
{
ifs .open ("TEST.TXT") ;
if (!ifs)
{
cout << "\nERROR" ;
return 1 ;
}
while (ifs) ifs.get (c) ;
ifs.clear();
ifs.close() ; // Strictly unnecessary, since stream has terminated. // Removing makes no difference.
}
return 0 ;
}


Or put the definition of 'ifs' inside the 'if' loop body
(before the open), so it will be destroyed and recreated
with each iteration of the loop.

-Mike
Jul 22 '05 #3

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

Similar topics

6
3480
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the correct output is that r = EOF. If it is taken to be a letter in the stream, then the output should be r = 0, as far as I can see. My compiler gives EOF.
0
1609
by: Rob Levine | last post by:
(This post also available at http://roblevine.blogspot.com/2004/11/frustrating-http-connection-behaviour.html in a slightly more readable format!) Hi All, I seem to be having a bit of a frustrating time with http connections in ..Net at the moment. Basically I am working on a project in which many subsystems use http to communicate with eachother. Once in a while, my .Net client will fail with a "System.Net.WebException: The underlying...
20
1311
by: Friedrich Dominicus | last post by:
I stumbled upon the following code (stripped to the minimum) #include <stdio.h> #include <stdlib.h> int main(void){ char *file_name = "t1.txt"; char ch; FILE *pf = fopen(file_name, "r+"); fputs("bla", pf);
12
41158
by: Michael Windsor | last post by:
I've been trying to integrate some PHP pages of my own with some existing code. The details of this are for the support forums for that code (where I have been asking questions), but I wonder if someone here can enlighten me as to why the problematic code is having the effect it is. For reasons I don't know, if the PHP version is 5 or greater, register_long_arrays is false and $_SESSION exists, the following statement is executed: ...
68
4606
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
285
8796
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
27
3131
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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
8743
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...
0
8622
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...
1
6177
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
5647
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();...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.