Quick help with ifstream (reading data from file)

Hey guys, I have .txt with data in it :

2 3
0 128 0
255 0 0
255 255 255
255 255 0
255 0 0
255 255 0

First two numbers (2 and 3) are used for outputting the results. However, I don't know how to read the rest of the numbers. This sounds easy, but for me not. I've never done such a reading. Bear in mind, I can't add extra data to help me read the file (can't edit the .txt file the way I want).

EDIT

So I kinda used one way to get the data, howver, I need to make it universal so no matter how many lines there are for the RGB color names my code would work anytime

1
2
3
    for(int i = 0; i < 6; i++)
        for(int j = 0; j < 3; j++)
            in >> RGB[j][i];


as you can see I typed numbers in for loops 6 and 3 but what if the .txt has not 6 lines but 100 lines of data
Last edited on
Use a std::vector to read an arbitrary amount of lines with push_back().

You can create a struct for the colour triplet: say
struct RGB{ unsigned short R,G,B };

Putting those together:
std::vector<RGB> mydata;
If your RGB data is encoded as 3 numbers to each line I'd probably create a struct to contain the three bits of data, and a container of like a std::vector to dynamically hold whatever number of lines the text file has. Reading the data file would be something like (pseudo-code):
1
2
3
4
while (read each line from the file)
{
   process each line into a struct, adding that struct to the vector
}
Topic archived. No new replies allowed.