473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problems with ifstream

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
5 8094
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

"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
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
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
> > >
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
11
by: Arturo DiDonna | last post by:
Hello everyone. I am trying to compile someone else code and I am stuck with compilation problems using the g++ 3.3 compiler. Basically, when compiling the following code, I get this error...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
3
by: Eric Lilja | last post by:
Hello, I'm creating a small utility for an online game. It involves parsing a text file of "tradesskill recipes" and inserting these recipes in a gui tree widget (similar to gui file browsers if...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
2
by: monkeon | last post by:
Hi, I'm pretty new to C++ I've been doing a course for a few months. I've been trying to design a simple console application that reads in floats from a text file and returns the average,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.