By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

Problems with ifstream

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Pete H wrote:
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

Jul 19 '05 #2

P: n/a

"Pete H" wrote:
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
Jul 19 '05 #3

P: n/a
On 6 Aug 2003 06:12:21 -0700, pe**@hufsoft.com (Pete H) wrote:
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
Jul 19 '05 #4

P: n/a
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message
news:su*********************@newssvr28.news.prodig y.com...
Pete H wrote:
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

Jul 19 '05 #5

P: n/a
> > >
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'


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
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.