Hey, I'm a bit rusty at c++ and I'm sure that I'm just missing something stupid, but I have a class that takes in an ifstream object in its constructor. In my main function I create an ifstream, open a file, then pass the ifstream object by reference into the class constructor. When I try to use the stream inside the constructor the is_open() method returns false and I can't read data from the file. Is it possible to pass an ifstream this way and have the file remain open so I can read from it, or would i be better off just passing the filename and creating/opening a stream inside the object.

thanks!

main function:

#include <fstream>

#include "headerFile.h"
 using namespace std;

 int main()
 {
 	ifstream Ideas;
 	ifstream Hints;
 	Ideas.open("Ideas.txt");
 	Hints.open("Hints.txt");
 	DataOne d1(Ideas, Hints);

 	return 0;
 }

DataOne class:

#include "headerFile.h"
#include <iostream>
#include <vector>

using namespace std;

DataOne::DataOne()
{
	
	

}

DataOne::DataOne(ifstream &ideas, ifstream &hints)
{
	//make sure both streams have open files
	
	if(!(ideas.is_open()))
	{
		cout << "IsOPen: " << ideas.is_open();
		cout << "Error: ideas ifstream- No file open!" << endl;
	}
	else if(!(hints.is_open()))
	{
		
		cout << "Error: hints ifstream- No file open!" << endl;
	}
	else
	{
	   //read from files
	}

	

}

Never Mind! The files I was trying to load somehow got moved out of the folder...

All fixed... Sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.