I am trying to open a file using the insertion stream operator Overloading.I want to open the file and conterminously read from it. The problem is that it reads one line always.either the first or last.
Is there a way to open a file keep it open and conterminously read from it using the insertion stream operator. I am totally confused now.
E.G
istream& operator >> (issrteam& in, HexColour& hex)
{
ifstream indData;
#include <iostream>
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::hex;
int main()
{
ifstream inData("text.txt");
int colour;
while (inData >> hex >> colour)
{
cout << hex << colour << endl;
}
return 0;
}
People I can read from a file in main.My problem is reading from a file in the insertion operator, I know how to convert and everything.I just need help with reading from a file within an insertion operator.The main doesnt open the file or anything.
Yes.But the problem is that the file is opened in main.In this instance I want everything to be done in the insertion operator, including opening the file.
The file should be created in the insertion operator and every time the operator is called it reads a new line in the file.With everything done in the operator
In this instance I want everything to be done in the insertion operator, including opening the file.
It seems you are very muddled about what it is you are attempting to do, as it doesn't make much sense.
In the original example, it is the extraction operator >> you meant, not the insertion operator <<. But in either case, these operators act upon an existing stream such as cin or an already opened ifstream. If you intend to open a stream (an input file) in the function, then it should just be an ordinary function, it doesn't need to override an operator.
I shouldn't really do this: I'm trying to guess at the requirements, but if my guess is wrong, then any attempt at a solution must inevitably be wrong too.