Please Help

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;

inData.open("text.txt");

while(!inData.oef())
{
in >> hex.colour;
}

return in;
}
i am completely lost.
Can you give us a 2 or 3-line example of the text file you are trying to read from please?
0x878789
0x21556456
0x1546545646

hope that helps
could you not just read each line in as a string then convert to hex?
My ,problem is not that.My problem is reading from a file using the insertion operator.
You don't need to open the file inside the function. Having opened it, inData isn't used for input, instead you use the parameter in.

Perhaps try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>

class HexColour
{
private:
    int colour;

public:
    friend std::istream & operator>>(std::istream & in, HexColour & hcol);
    friend std::ostream & operator<<(std::ostream & os, const HexColour & hcol);

};

std::istream & operator>>(std::istream & in, HexColour & hcol)
{
        in >> std::hex >> hcol.colour;
        return in;
}

std::ostream & operator<<(std::ostream & os, const HexColour & hcol)
{
        os << std::hex << hcol.colour;
        return os;
}

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;

int main()
{
    ifstream inData("text.txt");

    HexColour hcolour;

    while ( inData >>  hcolour)
    {
        cout <<  hcolour << endl;
    }

    return 0;
}
Though you don't necessarily need all that, this would do the same task:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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.

Please help.
Did you actually read this post ? http://www.cplusplus.com/forum/beginner/110031/#msg600650

If that doesn't help, then you need to be more clear about exactly what is your question.
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.

Anyway, here goes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>

class HexColour
{
private:
    // int colour;
    bool ok;
    std::ifstream in;

public:
    HexColour();
    ~HexColour() {}
    bool operator >> (int & n);
    
    bool is_ok() { return ok; }

};

HexColour::HexColour()
{
    in.open("D:\\temp\\text.txt");
    ok = in.is_open();
}

bool HexColour::operator >>(int & n)
{
    in >> std::hex >> n;
    ok = in;
    return ok;
}

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::hex;

int main()
{
    HexColour hcol;

    int number;

    while (hcol >> number)
    {
        cout << hex << number << endl;
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.