I was wondering. Is there a way to write something from one method in to a result file instead of outputting to screen?

To describe a little better, in pseudocode:

#include <iostream>
#include <fstream>

using namespace std;

class Inputoutput
{
private:
	ifstream fileOne;
	ofstream fileTwo;
        int i;
        //More variables here off course. Int's and stuff...

public:
	// In the original program this method 
        // has an output to screen:
	void outputFromFileToScreen()
	{
		fileOne.open("file.txt", ios::in);

                for(i = 0; i < 16 && !fileOne.eof(); i ++)
			{
				fileOne.get(chars[i]);
			} // end for
		infile.close();

		for(j = 0; j <i; j ++)
			{
				cout << (int) c[j];
			} // end for
	} // end methode
}; // end class

Now instead of outputting this to screen by calling this method from main I would like to do something like:

int main()
{
	ofstream resultMethod;

	resultMethod.open("resultFile.dat", ios::out);
	resultMethod << io.del3(); // Pass the output to resultFile instead of screen?
} // end main

But trying this just gives a "no operator "<<" matches these operands". And I'm at a loss. Trying to find a tutorial that shows how to output stuff from methods to a file but I can't find anything...?

you have to write your own operator for class Inputoutput:

ostream& operator << (ostream& os, const Inputoutput& io)
{
   // here you have to implement how you want to output
   os << io.someMember << "," << io.anotherMember();
   return os;
}

you might need to declare the new (global!) << operator as a friend to your
class, if it accesses private or protected members.
Once you created the override for your class, you can also use it with cout and cerr:

Inputoutput io;
cout << io << endl; // will be fine

Hmmm, okey. I'll give it a try! Thank you for pointing me in the right direction!

OK, so I tried doing this:

friend ostream& operator<<(ostream& out, IOmetoder &io);

// Code code
// code code code

ostream& operator<<(ostream& out, const Inputoutput &io)
{
	out << io.method1() << io.resultMethod(); // ***
	return out;
} // end metode

***Trying to say that when << is used with this, then the stuff done in method1 should be written to resultMethod. But then I get the "no operator "<<" matches these operands" between out and io.method1().

The thought was to then have the resultMethod() go something like this:

void resultMethod()
	{
		result.open("thisfile.dat");

		cout << io << endl;

		result.close();
	} // end metode

And then call this method from main.

But I've only done a couple of programs with operator overloading in the past and those did not invovle files. Just simple stuff like:

ostream& operator<< (ostream &out, Point &onePoint)
{
    out << "(" << onePoint.xvalue << ", " << onePoint.yvalue << ")";
    return out;
}

(Used for simple coordinates)

So again I'm at a loss... Trying to consult the books and online tutorials of the world to find a solution.

Sorry I should have been clearer:

out << val1 << meth2() << val3 << val4 << meth5();

for all your val_x and meth_y() the operators have to be defined also.
Don't worry too much cos for all the built-in types and some STL types they are already defined (everything you can print out with cout <<. But the methods have to return something that you can print. void won't work.

Ah! Okey. Now it's a little clearer. Thank you for helping!

Strictly speaking the operator(s) described above do not neccessarily write to files, but to streams. You can use them to write to strings, devices, and more without re-writing them. That's actually quite useful.

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.