(Latest Revision: Sun Feb 25 22:38:20 PST 2001 )
/* This is an example program to show the student an example of the coding techniques that might be used to load the contents of a file into an array, and to copy the contents of the array back out to the screen. */ #include <fstream.h> //includes iostream.h #include <assert.h> #include <string> const int rowMax = 50 ; const int colMax = 78 ; typedef char imageType[rowMax][colMax] ; /* #################### */ /* loadArt */ /* #################### */ /* This is a function that opens a file and copies the contents into a rectangular array called "image". */ void loadArt (imageType image) { /* start of function loadArt */ char trailChr ; int row, col ; string filename = "asciiArt" ; /* The next statement declares an input file stream variable "artFile", opens the file "asciiArt", and binds the open file to the stream variable. Note the use of the c_str() function to cast the C++ string into the equivalent C string. */ ifstream artFile(filename.c_str()) ; // Abort if the previous command failed. assert(artFile) ; /* The outer loop goes from row to row in the array -- top to bottom. */ for (row=0; row<rowMax; row++) { /* start of outer loop body */ /* The inner loop goes across the current row from column position 0 on the left to the last column position on the right. */ /* Precondition of this inner loop: we are ready to read the first char on a new line in the file. */ for (col=0; col<colMax; col++) { /* start of inner loop body */ /* Read the next char on the current line of the file and copy it into the correct position in the array. */ image[row][col] = artFile.get() ; } /* end of inner loop body */ /* We just finished reading a line of the file. Now get past the '\n' character at the end of the line, so the precondition will be true for the next repetition of the inner loop. */ trailChr = artFile.get() ; } /* end of outer loop body */ } /* end of function loadArt */ /* #################### */ /* writeArt */ /* #################### */ void writeArt (imageType image) { int row, col ; /* Print the image as-is */ /* The outer loop goes from row to row in the array -- top to bottom. */ for (row=0; row<rowMax; row++) { /* The inner loop goes across the current row from column position 0 on the left to the last column position on the right. */ for (col=0; col<colMax; col++) cout << image[row][col] ; /* Now that we have printed one row, move to the beginning of the next line on the screen. */ cout << endl ; } /* For fun: print the image upside down. */ for (row=rowMax-1; row>=0; row--) { for (col= colMax-1; col>= 0; col--) cout << image[row][col] ; cout << endl ; } /* For fun: print the image sideways. */ for (col=0; col<colMax; col++) { for (row=0; row<rowMax; row++) /* Note that we print an extra space after each element of the array, trying to improve the "aspect ratiol" of the display. */ cout << image[row][col] << ' '; cout << endl ; } } /* #################### */ /* MAIN */ /* #################### */ int main(void) { /* main program */ imageType image ; loadArt (image) ; writeArt(image) ; } /* main program */