how do i create a set method to get the fstream input file.

in need to be able to define the file location in the main method.

class a
{
private:
	ifstream fileOne;
	ofstream report1;
	
	
public:
	void setinput(std::ifstream& in);
	void setoutput(std::ofstream& out);
	
};

how can i code these set methods.

Thank you

Recommended Answers

All 5 Replies

how do i create a set method to get the fstream input file.

in need to be able to define the file location in the main method.

class a
{
private:
	ifstream fileOne;
	ofstream report1;
	
	
public:
	void setinput(std::ifstream& in);
	void setoutput(std::ofstream& out);
	
};

how can i code these set methods.

Use pointers:

class A // Better use capitalized names for your classes
{
public: // Better place public interface first
  A():inFile(0),report(0) {}
  void setinput(std::ifstream& in) { inFile = ∈ }
  void setoutput(std::ofstream& out)
  {
    outFile = &out;
  }
  void report();
  ...
private: // it's not so interesting for the class user
  ifstream*  inFile;
  ofstream* outFile;
};
void A::report()
{
  if (outFile) {
    ofstream& f = *outFile;
    f << ...;
    ...
  } // else no output file settings...
}

Be careful: you must provide these fstream existence while A::report is working. So it's not a very robust architecture (no matter with or w/o pointers).

commented: THank you for the Help:) +1

Thank you very much working perfectly i forgot about the pointers :)hehe

and one more thing how do i set the input file to "aa.txt" in the main method

oki i got it fixd.. had a small syntax error

and one more thing how do i set the input file to "aa.txt" in the main method

ifstream infile("aa.txt");
// or
infile.open("aa.txt");

;)hehe

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.