无法将数据从二维数组打印到C ++中的输出文件

时间:2014-11-03 16:39:25

标签: c++ arrays file fstream

此程序从.pgm图片文件中读取数据,然后将其打印到另一个.pgm输出文件。但是我无法使输出功能起作用。 output.pgm文件仍为空白。它编译,如果我在输入循环中添加cout语句,它表明数组正在保存数据,但如果我在输出循环中放置一个cout语句,它就不会打印任何内容。我认为这是将数组传递给outofArray函数的一个问题,但是对于我的生活,我不能把手指放在它上面。

#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();
}

1 个答案:

答案 0 :(得分:1)

这很可能是您的错误。

你有

            for (j = 0; j < inputColumns; i++) {

你需要

            for (j = 0; j < inputColumns; j++) {
                                       // ^^^^ j, not i.

intoArrayoutofArray中存在此错误。

相关问题