output file got problem

Hi everyone , what should i do ? my output file got problem , i expected that the output file have to be same as input file , I coding it to read the input file , but . . . it's not working .

[code]

#include <iostream>
#include <conio.h>
#include <fstream>
#include <stdlib.h>
#define ROWMAX 200
#define COLMAX 200
using namespace std;

ifstream inFile;
ofstream outFile;

// Prototype declarations
int avgFilter();

//====== main()
void main()
{
int greyVal[ROWMAX][COLMAX], // original values read from the pgm file
meanVal[ROWMAX][COLMAX]; // storing modified grey values for the output pgm file
char ftype[5]; // file type in pgm file: P2

// add other necessary variables declarations here
int ROW , COL , MAXval ;

// Open two different external files for reading and writing, and check files' status. Add your statements here.
inFile.open("Q.pgm");
outFile.open("newQ.txt");

if (inFile.fail() || outFile.fail()) {
cerr << " Program cannot read in the value ! " << endl;
_getch();
exit (-1); }

// start reading files, read file type: P2;
inFile >> ftype;

// read in values of 3 more integers here
inFile >> ROW >> COL >> MAXval ;

// by using nested for loops, write statements here to read grey values from input file to store in greyVal array
// read greyscale values
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
inFile >> greyVal[i][j]; }
}

// Output to external file, must be in exactly the same format as the input pgm file
outFile << ftype << endl; // This is the first line of the file.

// Write column and row to second line of the file
outFile << ROW << " " << COL << " " << endl ;

// Write the third line (255 as in Q.pgm)
outFile << MAXval << endl ;

// Write the contents of meanVal by using nested for loops

for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
outFile << meanVal[i][j]; }
}

outFile << endl;

inFile.close();
outFile.close();

cout << "Done! Please check output file.";
_getch();
}

[expected output file]

P2
10 11
255
0 0 0 0 0 0 0 0 0 0
0 0 0 150 150 150 150 0 0 0
0 0 255 0 0 0 0 255 0 0
0 200 0 0 0 0 0 0 200 0
0 200 0 0 0 0 0 0 200 0
0 200 0 0 0 0 0 0 200 0
0 200 0 0 0 255 0 0 200 0
0 200 0 0 0 0 255 0 200 0
0 0 255 0 0 0 0 255 0 0
0 0 0 150 150 150 150 0 255 0
0 0 0 0 0 0 0 0 0 0



please kindly assist me . thank you .
OK - the two biggest problems first.
(1) You INPUT data into array greyVal ... but you try to OUTPUT data from array meanVal (which has nothing in).

(2) Your file Q.pgm says 10 ROWS and 11 COLUMNS ... but if you count them you have 11 ROWS and 10 COLUMNS. Believe me: this matters!


Since I assume that eventually you will want to write from meanVal, I suggest that you put the following between input and output sections.
1
2
3
4
5
// TEMPORARY PLACEHOLDER; REPLACE LATER WITH ANY OTHER ROUTINE SETTING meanVal[][]
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
meanVal[i][j] = greyVal[i][j]; }
}


Make sure that your input file says 11 10 on the second line, not 10, 11.

You will need appropriate spacing in output file, so I suggest that you change this to
1
2
3
4
5
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
outFile << meanVal[i][j] << "  "; }  // <==== add a space after each entry
outFile << endl;   // <==== move here
}


Please use int main(), not void main().

Please use <cstsdlib>, not <stdlib.h>.

If you can avoid using VS-specific stuff that would be nice.

If you have further problems, please post the whole code and use code tags correctly.
Place a endl after the 'col loop' is done:
1
2
3
4
5
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
outFile << meanVal[i][j]; }
outFile << endl; // Note
}
lastchance

i try to use what u said

Please use int main(), not void main().

Please use <cstsdlib>, not <stdlib.h>.


and it give me error ,

i use void main() and <stdlib.h> because my lect already give those template to be used ..

also , without changing the header I still cant't get the expected output .

#include <iostream>
#include <conio.h>
#include <fstream>
#include <stdlib.h>
#define ROWMAX 200
#define COLMAX 200
using namespace std;

ifstream inFile;
ofstream outFile;

// Prototype declarations
int avgFilter();

//====== main()
void main()
{
int greyVal[ROWMAX][COLMAX], // original values read from the pgm file
meanVal[ROWMAX][COLMAX]; // storing modified grey values for the output pgm file
char ftype[5]; // file type in pgm file: P2

// add other necessary variables declarations here
int ROW , COL , MAXval ;

// Open two different external files for reading and writing, and check files' status. Add your statements here.
inFile.open("Q.pgm");
outFile.open("newQ.txt");

if (inFile.fail() || outFile.fail()) {
cerr << " Program cannot read in the value ! " << endl;
_getch();
exit (-1); }

// start reading files, read file type: P2;
inFile >> ftype;

// read in values of 3 more integers here
inFile >> ROW >> COL >> MAXval ;

// by using nested for loops, write statements here to read grey values from input file to store in greyVal array
// read greyscale values
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
inFile >> greyVal[i][j]; }
}

for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
meanVal[i][j] = greyVal[i][j]; }
}

// Output to external file, must be in exactly the same format as the input pgm file
outFile << ftype << endl; // This is the first line of the file.

// Write column and row to second line of the file
outFile << ROW << " " << COL << " " << endl ;

// Write the third line (255 as in Q.pgm)
outFile << MAXval << endl ;

// Write the contents of meanVal by using nested for loops

for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
outFile << meanVal[i][j] << " "; } // <==== add a space after each entry
outFile << endl; // <==== move here
}

outFile << endl;

inFile.close();
outFile.close();

cout << "Done! Please check output file.";
_getch();
}


i do that and get this in the external file

P2
10 11
255
0 0 0 0 0 0 0 0 0 0 0
0 0 150 150 150 150 0 0 0 0 0
255 0 0 0 0 255 0 0 0 200 0
0 0 0 0 0 200 0 0 200 0 0
0 0 0 0 200 0 0 200 0 0 0
0 0 0 200 0 0 200 0 0 0 255
0 0 200 0 0 200 0 0 0 0 255
0 200 0 0 0 255 0 0 0 0 255
0 0 0 0 0 150 150 150 150 0 255
0 0 0 0 0 0 0 0 0 0 0



one more thing ,

(2) Your file Q.pgm says 10 ROWS and 11 COLUMNS ... but if you count them you have 11 ROWS and 10 COLUMNS. Believe me: this matters!


i dont really understand that . bcause i think that Q.pgm absolutely says 10 ROWS and 11 COLUMNS and when i count them it's true .
coder

Place a endl after the 'col loop' is done:


thanks for that !
:relieved:

i got it . thanks everyone .
Topic archived. No new replies allowed.