Thread: reading arrays?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    reading arrays?

    hello, I'm new to C and have a pretty basic question. I have a 4 x 5 matrix saved in as a txt file and I can't figure out how to make my program stop reading the input file. I want the input to be stored in an array. I think there must be a way to do this by putting a while loop as the outer loop of my program but I'm not sure what condition to put in the loop.

    this is my code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    FILE *fin, *fout;
    
    main() {
           
           
           int A [4] [5] ;
           int r = 0;
           int c = 0;
           int r_max = 4;
           int c_max = 5;
           int i;
           	
    
                    fin = fopen("matrix.txt", "r");	  
                    fout = fopen("output.txt", "w");
    	 
                   
       
                   	for(i = 0; r < r_max; i++) {
    	for(j = 0; c < c_max; j++) {
    	
                    fscanf(fin, "%d", &A[i][j]);
    	fprintf(fout, "%d ", A[i][j]);
    	}
    	fprintf(fout, "\n");
    		}
    but it just reads the input file forever and crashes, any help with this would be greatly appreciated, thank you.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Look at your for loops.. you are incrementing i, but checking the value of r. Inside, you are incrementing j, but checking the value of c, that doesn't make sense.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    ahhh true, thank you, i didn't notice that, i changed that in my code and also added a while loop, but my output is still going on forever, instead of stopping after reading the matrix, any idea why? here's my revised code. Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    FILE *fin, *fout;
    
    main() {
           
           
           int A [3] [4] ;
           int r = 0;
           int c = 0;
           int r_max = 4;
           int c_max = 5;
           int i;
           int j;
           int k;
    
                    fin = fopen("matrix.txt", "r");	  
                    fout = fopen("output.txt", "w");
    	 
                   
       
      	while(fscanf(fin, "%d", &k)  != EOF ) {
          
        for(r = 0; r < r_max; r++) {
    	for(c = 0; c < c_max; c++) {
    	
        fscanf(fin, "%d", &A[i][j]);
    	fprintf(fout, "%d ", A[i][j]);
    	}
    	fprintf(fout, "\n");
    		}
    
          printf("%d",A[2][2]);
          
          getchar();
          
           }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  2. Reading char arrays from a binary file
    By eaane74 in forum C++ Programming
    Replies: 17
    Last Post: 11-20-2007, 04:19 PM
  3. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM
  4. reading arrays from files
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 01:23 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM