At the moment this program reads data from a .pgm picture file and then prints it to another .pgm output file. However I cannot get the output function to work. The output.pgm file is still blank. It compiles, and if I add cout statements in the input loop it shows that the array is holding the data, but if I put a cout statement in the output loop it doesn't print anything. I assume this is an issue with passing the array to the outofArray function, but for the life of me I can't put my finger on it.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//the maximum size of the photo to be stored in the array
const int rows = 512;
const int columns = 512;
//the multidimensional array's row value has to be passed as a parameter.
//the intoArray function opens the input file (a .pgm file), reads the amount of rows and columns,
//then reads the pixel data into a 512x512 array and closes the input filestream
void intoArray (int argc, char* argv[], int photoArray[][columns], int &inputRows, int &inputColumns, int rows);
//this function prints the data from the picture array into the output file
void outofArray (int argc, char* argv[], int photoArray[][columns], int inputRows, int inputColumns, int rows);
int main(int argc, char* argv[]) {
int photoArray[rows][columns];
int inputRows, inputColumns;
if (argc != 3) {
cout<<"Error: Incorrect number of parameters. "<<endl;
return -1;
}
intoArray (argc, argv, photoArray, inputRows, inputColumns, rows);
outofArray (argc, argv, photoArray, inputRows, inputColumns, rows);
//just testing for proper fileread (this also does not print anything for some reason)
cout<<"rows: "<<inputRows<<endl
<<"columns: "<<inputColumns<<endl;
return 0;
}
void intoArray (int argc, char *argv[], int photoArray[][columns], int &inputRows, int &inputColumns, int rows) {
ifstream pgmIn;
string p2;
int twofivefive, i, j;
if (argc != 3)
cout<<"Error: Incorrect number of parameters. "<<endl;
//open the input filestream
pgmIn.open(argv[1]);
//check if file opened properly
if (pgmIn.fail()) {
perror (argv[1]);
}
//read the initial data from the pgm file. the p2 and twofivefive variables
//can be ignored, as they are not needed, but need to be read in order to get to the
//actual picture data.
while (pgmIn>>p2>>inputColumns>>inputRows>>twofivefive) {
//once again just checking for proper fileread.
//i'm not sure what i did, but before the function became impossible to call,
//only the p2 string was being read and it printed just fine, but the rest of
//the values were never read.
cout<<p2<<" "<<endl<<inputColumns<<" "<<inputRows<<" "<<endl<<twofivefive<<endl;
//this actually reads the picture data into the array.
for (i = 0; i < inputRows; i++) {
for (j = 0; j < inputColumns; i++) {
pgmIn>>photoArray[i][j];
//I put a cout statement here followed by a cout.flush() and it printed the data entries
//just fine, so I assume it is properly reading the file
}
}
}
pgmIn.close();
}
void outofArray (int argc, char* argv[], int photoArray[][columns], int inputRows, int inputColumns, int rows) {
ofstream pgmOut;
int i, j;
if (argc != 3)
cout<<"Error: Incorrect number of parameters. "<<endl;
//open the output filestream
pgmOut.open(argv[2]);
//check if file opened properly
if (pgmOut.fail())
perror (argv[2]);
//prints the necessary data for the .pgm file to be read
pgmOut<<"P2"<<endl<<inputColumns<<" "<<inputRows<<endl<<"255"<<endl;
//prints data to output file
for(i = 0; i < inputRows; i++) {
for(j = 0; j < inputColumns; i++) {
pgmOut<<photoArray[i][j]<<" ";
//if I put a cout statement here as I did in the previous function,
//it doesn't print anything. This leads me to believe that there is an
//issue with passing my array and its contents to this function
}
}
pgmOut.close();
}
This is most likely your error.
You have
for (j = 0; j < inputColumns; i++) {
You need
for (j = 0; j < inputColumns; j++) {
// ^^^^ j, not i.
This error exists in intoArray
and outofArray
.