struct RGB {
int r, g, b;
};
RGB operator>>(std::ifstream& in, RGB& rgb) {
in >> rgb.r;
in >> rgb.g;
in >> rgb.b;
return rgb;
}
std::ostream& operator<<(std::ostream &out, RGB& rgb) {
out << rgb.r << " ";
out << rgb.g << " ";
out << rgb.b << std::endl;
return out;
}
int main()
{
ifstream myFile;
myFile.open("Image01.ppm", std::ifstream::binary);
//Check for error
if (myFile.fail())
{
cerr << "Error loading file" << endl;
} else {
char magic[5];
int width, heigth;
int pixelValue;
myFile >> magic >> width >> heigth >> pixelValue;
cout << magic << endl << width << endl << heigth << endl << pixelValue << endl;
RGB rgb;
int size = width*heigth;
for (int i = 0;i < size;i++) {
myFile >> rgb;
std::cout << rgb;
}
}
myFile.close();
system("pause");
return 0;
}
Hello guys , I have been trying to read a ppm file image but I have some problems , when I read a txt file I have created with the imageType ,width,height and pixelValue and some RGB triplets everything is ok , when I actually try to read the ppm file it reads the imageType ,width,height and pixelValue however when I want it to print the pixels it just prints -858993460 forever, when I change the struct so the rgb value are chars and not ints , I got some weird symbols printed .So how do we print and store binary data in C++ and what am I doing wrong here?