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

B

bruce varley

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.

(e-mail address removed)
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 ;
}
 
T

tom_usenet

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
 
M

Mike Wahler

bruce varley said:
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.

(e-mail address removed)
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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top