File I/O with 2D arrays

This is a discussion on File I/O with 2D arrays within the C Programming forums, part of the General Programming Boards category; This is a program to read in a file of 2D arrays. I have gotten the code to work (option ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Question File I/O with 2D arrays

    This is a program to read in a file of 2D arrays. I have gotten the code to work (option 1) but whenever I change the input file to 3 by 3, it stops working. Any idea why? Tks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX_STR_LENGTH 255
    
    typedef struct {
        int rows;
        int cols;
        int** element;
    } matrix;
    
    void displayMenu(void);
    void addMatrix(char *filename, matrix *matrices, matrix *resMatrix);
    int readMatrix(char *filename, matrix *matrices, int num);
    void freeMalloc(matrix *matrices, int num);
    
    int main(void){
    
        int choice, n;
        char inputBuffer[MAX_STR_LENGTH], filename[MAX_STR_LENGTH];
        matrix matrices[2];
        matrix resMatrix[1];
    
        do{
    		displayMenu();
    		do{
    			gets(inputBuffer);
    			n = sscanf(inputBuffer, "%d", &choice);
    		}while (n != 1);
    		puts("");
    
    		switch(choice){
    		case 1:
    			puts("System >> Enter matrices for addition operation:");
             printf("User >> ");
             fflush(stdin);
    			gets(inputBuffer);
    			sscanf(inputBuffer, "%s", filename);
             addMatrix(filename, matrices, resMatrix);
             break;
    		case 2:
    			break;
    		case 3:
    			break;
    		case 4:
    			break;
    		case 5:
    			break;
    		default:
    			puts("Please enter a choice from 1 to 5.");
    		}
    	}while(choice != 5);
    
    	puts("Exiting Program ... ...");
    	
    	freeMalloc(matrices, 2);
    	
    	return 0;
    }
    
    void displayMenu(void){
        puts("");
        puts("Welcome to simple matrix program.");
        puts("Please select an option.");
        puts("");
        puts("(1) Add two matrices");
        puts("(2) Subtract two matrices");
        puts("(3) Multiplication of two matrices");
        puts("(4) Assign a matrix to another");
        puts("(5) Exit");
        puts("");
        printf("Your choice: ");
    }
    
    int readMatrix(char *filename, matrix *matrices, int num){
        int read, orders = 1;
        int i, j, rows, cols;
        char inputBuffer[MAX_STR_LENGTH];
        FILE *fp;
    
        if ((fp = fopen(filename, "r")) != NULL){
    		for (read = 0; read < num; read++) {
    
    	     do {
    	     fgets(inputBuffer, MAX_STR_LENGTH, fp);
    	     } while(strstr(inputBuffer, "<matrix>") == NULL);
    
    	     fgets(inputBuffer, MAX_STR_LENGTH, fp);
    	     matrices[read].rows = rows = atoi(strchr(inputBuffer, '=') + 2);
    
    	     fgets(inputBuffer, MAX_STR_LENGTH, fp);
    	     matrices[read].cols = cols = atoi(strchr(inputBuffer, '=') + 2);
    
    	     matrices[read].element = (int**) malloc(sizeof(int) * rows);
    
            printf("System >> Displaying matrix %d\n", orders);
            orders++;
    
    	     for (i = 0; i < rows; i++) {
    	       matrices[read].element[i] = (int*) malloc(sizeof(int) * cols);
    	       for (j = 0; j < cols; j++) {
    	       	fscanf(fp, "%d", &matrices[read].element[i][j]);
                printf("%d ", matrices[read].element[i][j]);
              }
    	       printf("\n");
    	     }
    
    	     do {
    	       fgets(inputBuffer, MAX_STR_LENGTH, fp);
    	     } while (strstr(inputBuffer, "</matrix>") == NULL);
          }
        }
        else {
          printf("%s do not exist!", filename);
          puts("");
          return orders;
          }
        fclose(fp);
        return orders;
    }
    
    void freeMalloc(matrix *matrices, int num) {
    	int current, i;
    
    	for (current = 0; current < num; current++) {
    		for (i = 0; i < matrices[current].rows; i++)
    			free(matrices[current].element[i]);
    		free(matrices[current].element);
    	}
    }
    
    void addMatrix(char *filename, matrix *matrices, matrix *resMatrix){
    
        int i, j, rows, cols;
    
        if (readMatrix(filename, matrices, 2) != 1){
        resMatrix[0].rows = rows = matrices[0].rows;
        resMatrix[0].rows = cols = matrices[0].cols;
        puts("System >> Displaying resultant matrix after addition");
    
        resMatrix[0].element = (int**) malloc(sizeof(int) * (rows+1));
    
        for(i = 0; i < rows; i++){
          if (i != 0) printf("\n");
          for(j = 0; j < cols; j++){
            resMatrix[0].element[i][j] = (matrices[0].element[i][j] +
            matrices[1].element[i][j]);
            printf("%d ", resMatrix[0].element[i][j]);
          }
        }}
    }
    The 2 by 2 array file matrix2-2.txt
    Code:
    <matrix>
         rows = 2
         cols = 2
    
         1    2
         1    2
    </matrix>
    
    <matrix>
         rows = 2
         cols = 2
    
         2    3
         2    3
    </matrix>
    The 3 by 3 array file matrix3-3.txt
    Code:
    <matrix>
         rows = 3
         cols = 3
    
         1    2    3
         1    2    3
         1    2    3
    </matrix>
    
    <matrix>
         rows = 3
         cols = 3
    
         2    3    4
         2    3    4
         2    3    4
    </matrix>

  2. #2
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    Firstly, do a board search on gets() and fflush(stdin) and read about why you shouldn't use them.

    Secondly, you didn't state what the problem was, i.e. what doesn't work, or what happens that's not supposed to.

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    3 X 3 Problem

    Not that I understand all of the intricasies of your code but it appears to me that the only problem with it is that in your call to the read_matrix function in the add_matrix function, you have the number of rows to read hardcoded to be 2. This number should be changed to accept the number of rows desired and in your case should be changed to 3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21