Please analyze the following code and tell me why all of the \"ifs\" (not if sta
ID: 3768583 • Letter: P
Question
Please analyze the following code and tell me why all of the "ifs" (not if statements) after ifstream ifs(fn) give errors. Thank you!
string get_filename( )
{
bool ok = false;
string fn = "";
do
{
if (fn.size()==0) cout << "filename? ";
else cout << "can't open "<<fn<<". re-enter filename: ";
cin >> fn;
cout << "trying to open file " << fn << endl;
ifstream ifs(fn); // make sure we can open the file.
if (ifs) ok = true;
if (ok)
cout << "file opened OK."<<endl;
else
cout << "problem opening file"<<endl;
} while (!ok);
return fn;
}
//............................write these two functions:
//readPGM: opens file, reads header, allocates pixel array, reads in pixels.
//note that nc, nr, mv and pix are all passed by reference.
// Precondition: pix ptr must be NULL or point do an old pixel buffer which will be deleted.
// pix ptr of NULL means no memory allocated before this.
// if pix is not NULL, then this function will delete the old memory and allocate new memory.
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
// open the file
string img_id;
delete [] pix;
ifstream ifs(fname);
if (!ifs) // deal with it if we can't open it
{
cerr << "error: couldn't open "<<fname<<" for reading"<<endl;
nc=0;
nr=0;
mv = 0;
pix = NULL;
}
// read header
ifs >> img_id >> nc >> nr >> mv;
if (img_id != "P2") // deal with it if not a PGM
{
cerr << "error: file "<<fname<<" is not a PGM. ID is "<< img_id << endl;
nc=0;
nr=0;
mv = 0;
pix = NULL;
}
cout << "img hdr: "<< img_id << " " << nc << " " << nr << " "<<mv<<endl;
// allocate an array
if ( nc * nr <= 0 || nc * nr > 1e9)
{
cerr << "bad header: unexpected image size: " << nc << " by " << nr <<endl;
nc=0;
nr=0;
mv = 0;
pix = NULL;
}
pix = new int [ nc * nr ];
// read in the pixel array and put in array
for (int r=0;r<nr;r++)
{
for (int c=0;c<nc;c++)
{
int ix = r*nc + c;
ifs >> pix[ix];
if (c < 10 && r < 10)
{
cout << pix[ix]<<" ";
if (c == 9) cout << endl;
}
}
}
// close the file
ifs.close();
}
//.......................................
//writePGM: opens output file, outputs header line, outputs pixel array.
void writePGM(string fname, int nc, int nr, int mv, int *pix)
{
// open a file for writing
ofstream ofs(fname);
// write the pgm header
ofs << "P2" << " " << nc << " " << nr << " " << mv << endl;
// write out the pixels
for (int r=0;r<nr;r++)
{
for (int c=0;c<nc;c++)
{
int ix = r*nc + c;
ofs << pix[ix] << " ";
}
ofs << endl;
}
// close the file
ofs.close();
}
//.......................................
//negatePGM: note that pix is passed by value. but since it is a pointer,
//we can still modify the pixel array by indexing it. why?
void negatePGM(int nc, int nr, int maxval, int *pix)
{
for (int r=0;r<nr;r++)
for (int c=0;c<nc;c++)
pix[r*nc + c] = maxval - pix[r*nc + c];
}
Explanation / Answer
In readPGM() you need to allocate memory, then read data.
You allocate memory by using the new[] operator as in:
pix = new int[1000];
Then in main() don't forget to delete[] the pix pointer, to de-allocate the memory:
delete[] pix; // Need to delete the pixel array
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.