Text file reading into vector

Hello folks.

My aim is to read the contents of a single txt file into a vector which structure is "r g b" line by line.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct TDATA{
    int r,g,b;
};

int main(){
    vector<TDATA> image;

    TDATA puffer;
    unsigned int i=0;

    ifstream keptxt("kep.txt");
    if(keptxt.is_open()){
        while(!keptxt.eof()){
            keptxt >> (int)puffer.r >> (int)puffer.g >> (int)puffer.b;
            image.push(puffer);
            cout << ++i << ".line: " << puffer.r << "-" << puffer.g << "-" << puffer.b << endl;
        }
    }

    return 0;
}


What do you think, how should I do it?
Delete all c-type casts.
a) They are not needed here
b) Use C++ style casts in C++
Thanks for your quick response!
So you mean (int)puffer.r -> int(puffer.r)? I tried that, but it did not helped.
Anyway, the given error is:
error: ambiguous overload for ‘operator>>’ in ‘keptxt >> puffer.TDATA::r’|
(at line that starts with "keptxt >>...")
no, I mean keptxt >> puffer.r >> puffer.g >> puffer.b;
Also use of eof() is error-prone. Write:
1
2
3
while(keptxt >> puffer.r >> puffer.g >> puffer.b) {
    image.push(puffer);
    //... 

And it is push_back(), not push() :image.push_back(puffer);
Last edited on
Yes, I have already noticed that I unfortunately mistyped push.
Anyway, You Are The Hero Of The Day. Thank you sir!
Topic archived. No new replies allowed.