Problems with ifstream

P

Pete H

I am trying to use the same ifstream object to open two files. I will
eventually want to open many with a loop. The first file opens fine,
but the second has a problem. In my test I try to open two .txt files,
the first

text.txt reads:

First seems to work...

text2.txt reads:

As does second...

Here is my code to read these files and print the contents:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char c;
string szc;

ifstream ifsin;

ifsin.open("text.txt",ios::in);
if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
cout << szc << endl;
}

ifsin.close();

if(!ifsin.is_open()) cout << "it give me not is open" << endl;

ifsin.open("text2.txt",ios::in);

if(ifsin.eof()) cout << "it give me an eof" << endl;
if(ifsin.is_open()) cout << "it give me is open" << endl;

if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
cout << szc << endl;
}

ifsin.close();

return 0;
}


The output I get looks like this:

First seems to work...
it give me not is open
it give me an eof
it give me is open
First seems to work...

What I want is:

First seems to work...
it give me not is open
it give me is open
As does second...

Any ideas?
 
T

Thomas Matthews

Pete said:
I am trying to use the same ifstream object to open two files. I will
eventually want to open many with a loop. The first file opens fine,
but the second has a problem. In my test I try to open two .txt files,
the first

text.txt reads:

First seems to work...

text2.txt reads:

As does second...

Here is my code to read these files and print the contents:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char * argv[])
{
char c;
string szc;

ifstream ifsin;

ifsin.open("text.txt",ios::in);
if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
getline(ifsin, szc, '\n');
cout << szc << endl;
}

ifsin.close();
Add this line:
ifsin.clear();
I believe you need to clear the I/O states before using it again.
if(!ifsin.is_open()) cout << "it give me not is open" << endl;

ifsin.open("text2.txt",ios::in);

if(ifsin.eof()) cout << "it give me an eof" << endl;
if(ifsin.is_open()) cout << "it give me is open" << endl;

if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
cout << szc << endl;
}

ifsin.close();

return 0;
}


The output I get looks like this:

First seems to work...
it give me not is open
it give me an eof
it give me is open
First seems to work...

What I want is:

First seems to work...
it give me not is open
it give me is open
As does second...

Any ideas?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
P

Patrick Frankenberger

Pete H said:
I am trying to use the same ifstream object to open two files. I will
eventually want to open many with a loop. The first file opens fine,
but the second has a problem. In my test I try to open two .txt files,
the first

open() and close() don't change the state of the stream, they only change
the underlying streambuffer. To change the streamstate you have to call
clear().

HTH,
Patrick
 
T

tom_usenet

I am trying to use the same ifstream object to open two files. I will
eventually want to open many with a loop. The first file opens fine,
but the second has a problem. In my test I try to open two .txt files,
the first

text.txt reads:

First seems to work...

text2.txt reads:

As does second...

Here is my code to read these files and print the contents:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char c;
string szc;

ifstream ifsin;

ifsin.open("text.txt",ios::in);
if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
cout << szc << endl;
}

ifsin.close();

ifsin.clear(); //clear error state of stream
Any ideas?

The above should fix it.

Tom
 
M

Mike Wahler

Thomas Matthews said:
Pete said:
I am trying to use the same ifstream object to open two files. I will
eventually want to open many with a loop. The first file opens fine,
but the second has a problem. In my test I try to open two .txt files,
the first

text.txt reads:

First seems to work...

text2.txt reads:

As does second...

Here is my code to read these files and print the contents:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char * argv[])
{
char c;
string szc;

ifstream ifsin;

ifsin.open("text.txt",ios::in);
if(!ifsin)
cerr << "\nUnable to open 'text.in' for input.";
else
{
getline(ifsin,szc);
getline(ifsin, szc, '\n');
cout << szc << endl;
}

ifsin.close();
Add this line:
ifsin.clear();
I believe you need to clear the I/O states before using it again.

If the last input sets failbit or eofbit, then
the 'clear()' should be done before the 'close()'
or the 'close()' fails. So putting the 'clear()'
before the 'close()' should 'cover all your bases'

$.02,
-Mike
 
J

John Harrison

If the last input sets failbit or eofbit, then
the 'clear()' should be done before the 'close()'
or the 'close()' fails. So putting the 'clear()'
before the 'close()' should 'cover all your bases'

Calling close followed by open on a fstream does not clear the stream state.
Is it generally accepted that this is an oversight on the part of the
standard? It seems awfully counter intuitive to me (and many other newbies
who post here).

john
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top