Member Avatar for Zaina jee

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.

Recommended Answers

All 2 Replies

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

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

Member Avatar for Zaina jee

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.