// file: $(LAB 07)/mfuncts.cc // // this file containts support functions to read a matrix // from a file. // // modified: // 20181116 (JP): initial version // // local include files // #include "Matrix.h" //============================================================================= // function: load // // arguments: // MATRIX& mat: (output) matrix loaded from the file // FILE* fp: (input) an open file pointer // // return: a boolean value indicating success // // Note that we will keep this simple and read the file multiple times. // bool load(MATRIX& mat_a, FILE* fp_a) { // declare local variables // char buf[MAX_LINE_LEN]; char line_buf[MAX_LINE_LEN]; // rewind the file to be safe // rewind(fp_a); // get the number of rows: // read the file line by line and count the lines // mat_a.nrows = (long)0; while (fgets(line_buf, MAX_LINE_LEN - 1, fp_a) != (char*)NULL) { mat_a.nrows++; } // create space: // ncols is a vector containing the dimension of each row // data contains the matrix data, implemented as a vector of vectors. // mat_a.ncols = (long*)new long[mat_a.nrows]; mat_a.data = new float*[mat_a.nrows]; // rewind the file again // rewind(fp_a); // loop over all rows // for (long i = 0; i < mat_a.nrows; i++) { // read a line into a buffer // if (fgets(line_buf, MAX_LINE_LEN - 1, fp_a) == (char*)NULL) { return false; } // strip the newline (we don't really need to do this) // line_buf[strlen(line_buf) - 1] = (char)NULL; // copy the buffer: // strtok overwrites the input string, so we need to keep a copy // of it. we will operate on the copy. // strcpy(buf, line_buf); // initalize strtok by reading the first col // mat_a.ncols[i] = 0; char* ptr = strtok(buf, DELIMITERS); if (ptr == (char*)NULL) { fprintf(stdout, "%s: error converting a line (%s)\n", "load()", buf); return false; } else { mat_a.ncols[i]++; } // read the rest of the columns: // note that we must replace the input string with NULL // while((ptr = strtok((char*)NULL, DELIMITERS)) != (char*)NULL) { mat_a.ncols[i]++; } // copy the input into a temporary buffer again so we can use strtok // strcpy(buf, line_buf); // iterate over the data again and convert the actual data values // mat_a.data[i] = new float[mat_a.ncols[i]]; long j = (long)0; ptr = strtok(buf, DELIMITERS); if (ptr == (char*)NULL) { fprintf(stdout, "%s: error converting a line (%s)\n", "load()", buf); return false; } else { mat_a.data[i][j] = atof(ptr); j++; } // read the rest of the columns // while((ptr = strtok((char*)NULL, DELIMITERS)) != (char*)NULL) { mat_a.data[i][j] = atof(ptr); j++; } // end of loop - we have completed processing the line // } // exit gracefully // return true; } //============================================================================= // function: display // // arguments: // FILE* fp: (output) an open file pointer // MATRIX& mat: (input) matrix to be displayed // char* delim: (input) a delimiter string // // return: a boolean value indicating success // bool display(FILE* fp_a, MATRIX& mat_a, char* delim_a) { // display a delimiter string // fprintf(fp_a, "%s\n", delim_a); // display the number of rows // fprintf(fp_a, " number of rows is: %d\n", mat_a.nrows); // iterate over all the columns // for (long i = 0; i < mat_a.nrows; i++) { // display the number of cols // fprintf(fp_a, " %d: ", mat_a.ncols[i]); // display the values // for (long j = 0; j < mat_a.ncols[i]; j++) { fprintf(fp_a, "%6.2f ", mat_a.data[i][j]); } // add a linefeed // fprintf(fp_a, "\n"); } // exit gracefully // return true; }