0

I am new to filing and have not much idea about it.

I have written a code that tries to read an ASCII .pgm file named pic.pgm and writes it as mypic.pgm file:

#include <fstream>
const int MAXHEIGHT=221;
unsigned char *bitmap[MAXHEIGHT]={'\0'} ;// pointers to each pixel row
int main()
{
    int width=201, height=221;
    std::ifstream ifile("pic.pgm",std::ios::in);
    std::ofstream ofile("mypic.pgm",std::ios::out);
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<width;++j)
            ifile.read(bitmap[i][j],sizeof(bitmap));
    }
    ofile << "P2\n" << width << " " << height << "\n255\n";
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<width;++j)
            ofile<<bitmap[i][j];
    }
}

By my code has some errors. I will be thankful if anybody help me in correcting it.

2
Contributors
2
Replies
15
Views
4 Years
Discussion Span
Last Post by Zaina jee
0

ifile.read(bitmap[i][j],sizeof(bitmap));

How much data are you intending to read each time you execute this line?

0

I want to read each pixel of an image in it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.