0

I am attempting to write code for reading a matrix reader that will read in a matrix from a redirected file (so FILE functions will not be used in my program), store it in a dynamically created array and then print out to console.

Important note Array is dynamic (meaning that the dimensions are obtained by reading the entire file and calculating the # of rows & columns.

I have tried writing code 2 different ways to do this and both result in wrong output:

Version A:

while(ch != EOF) {
  ch = fgetc(stdin);

  if(ch == ' ') {
    fields++;
  }
  if(ch == '\n') {
    rows++;
  }

}

Version B:

do {
  c=getchar();

  if(c == ' '){
    fields++;
  }

} while (c != EOF); 

Question

  1. Does while(ch != EOF) or while(c=getchar() != EOF) mean while it does not hit end of a LINE, or end of the FILE?

I have had little luck with Version B shown above. When used on test file:

10 20 30 40
50 60 70 80
90 10 20 30
40 50 60 70

I get output:

50 60 70 80
90 10 20 30
40 50 60 70
70 70 70 70

I think the problem I am having here is that whenever I am reading my file once it hits EOF it breaks out of the loop, and then it is on the 2nd line of the file, hence why the output starts at the 2nd line, and then duplicates the last number X times to fill the rest of the matrix.

Is my goal here achievable with my current methods? Here is all of my code, Thanks.

#include <stdio.h>
#include <stdlib.h>

int main() {
  int rows=0;
  int fields=1;
  int i=0;
  int j=0;
  int c=0;
  char ch = '\0';

/*  while(ch != EOF) {
    ch = fgetc(stdin);
    if(ch == ' ') {
      fields++;
    }
    if(ch == '\n') {
      rows++;
    }
  } */

   do {
    c=getchar();
    if(c == ' '){
      fields++;
    }

  } while (c != 10);


  int **array;
  array = malloc(fields * sizeof(int *));

  if(array == NULL) {
    printf("Out of memory\n");
    exit(1);
  }

  for(i = 0; i < fields; i++) {
    array[i] = malloc(fields * sizeof(int));
    if(array[i] == NULL) {
      printf("Out of memory\n");
      exit(1);
    }

  }

  for(i = 0; i < fields; i++) {
    for(j = 0; j < 4; j++) {
      int k;
      scanf("%d", &k);
      array[i][j] = k;
      printf("%d ", k);
    }
    printf("\n");
  }
} 
7
  • Actually while(c=getchar() != EOF) should be while((c=getchar()) != EOF) add parentheses. first assign then compare, you are assigning result of comparison to C then comparison returns 0 your loop breaks Commented Apr 7, 2014 at 15:21
  • btw check your question once again, your while conditions in question is not corresponding to the code pieces you shown. Commented Apr 7, 2014 at 15:25
  • Yeah I recently changed while (c != EOF) to while(c != 10) However I do believe that these are technically doing the same thing. Right? Also, adding a parenthesis resulting in my output being all 0's, and it output a 4x13 matrix so yeah, any idea what is going on here now? Commented Apr 7, 2014 at 15:35
  • No first EOF != 10, second my second comment means you are not using "while(c=getchar() != EOF)" Commented Apr 7, 2014 at 15:36
  • Using code: while((c=getchar()) != EOF) and then fields++ if c == ' ' and rows++ if c == '\n' results in a huge matrix of only 0's. Commented Apr 7, 2014 at 15:41

1 Answer 1

1
#include <stdio.h>
#include <stdlib.h>

int main(){
    int rows = 0;
    int cols = 0;
    int fields = 0;
    int i, j, c, n, status;;
    int **array;
    char ch;

    array = malloc((rows+1) * sizeof(int*));//check omit
    array[rows] = NULL;

    while(0<(status = scanf("%d%c", &n, &ch))){
        if(status>1){
            if(cols == fields){
                array[rows] = realloc(array[rows], (fields=cols+1)*sizeof(int));
            }
            array[rows][cols++] = n;
            if (ch == '\n'){
                array = realloc(array, (++rows+1) * sizeof(int*));
                array[rows] = malloc(fields*sizeof(int));
                cols = 0;
            }
        } else {
            array[rows][cols++] = n;
            break;
        }
    }
    if(cols == 0){
        free(array[rows--]);
    }
    for(i=0;i<=rows;++i){
        for(j=0;j<fields;++j){
            printf("%d ", array[i][j]);
        }
        printf("\n");
        free(array[i]);
    }
    free(array);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.