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?
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?