Loading Comma Separated Data from a File

Hi there. I'm having some difficulty with a little project of mine. Basically, I've written some member functions to manipulate the pixel values of a black and white image. But I'm unable to use them until I figure out how to import the image.

I have saved the image in a text file like so, where 0 = a black pixel, 255 = a white pixel and the rest are values of grey:

15,88,200
172,56,111

So on and so forth. My problem is, how can I parse the data so it can be loaded into a 2D array for me to work with? I want to make use of the commas as separators, but how can I do this? Here is some of what I have so far. Inside the while loop, I want to make the assignment of the file to imgPix.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool loadData(int **imgPix, string fileName) {
	
	ifstream fin("fileName.txt");
	
	if (!fin) {
		cout << "Could not open " << fileName;
		return false;
	}
	
	while (!fileName.eof) {
		
	}
}


Any assistance would be greatly appreciated!
You can use getline() ( http://cplusplus.com/reference/string/string/getline/ ) to read each number into a string, just use ',' as the thrird, optional parameter.

 
getline(someIfstream, someString, ',');


Then just use stringstreams ( http://cplusplus.com/reference/sstream/stringstream/ ) to convert the string to a number.

1
2
3
std::sstream converter;
converter << someString;
converter >> someInt;
Topic archived. No new replies allowed.