Reading a 2D array from a file, compiles but doesnt work properly

This is a discussion on Reading a 2D array from a file, compiles but doesnt work properly within the C Programming forums, part of the General Programming Boards category; Hello, im beginning to write a program to calculate the determinant of a 3x3 matrix - i need to read ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    Reading a 2D array from a file, compiles but doesnt work properly

    Hello, im beginning to write a program to calculate the determinant of a 3x3 matrix - i need to read the elements of the matrix from an external file matrix.dat (contains 9 numbers, 3 rows, 3 columns seperated by spaces)

    I have written the first part of the program - reading from the file: It compiles but doesnt work, i got it to print a value from the matrix to see if it read it but when i run the program it says 0.00000, implying a problem

    where have i gone wrong? thanks so much!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /*need to use an external function to computer det of a minor (2x2 matrix)*/
    /*Need to read from a file named matrix.dat*/
    
    /* Prototype your function here, also make sure you change all of these comments*/
    int ReadFromFile(float fullmatrix[3][3], FILE *input);
    
    int main(int argc, char* argv[])
    {
        FILE         *input;
        int           i, j;  
        float         fullmatrix[3][3];
        const char    inp_fn[]="matrix.dat";
        
        /* Open files */
        input = fopen(inp_fn, "r");
        /* Check the pointers to files are not NULL, also check matrix has correct number of elements */
        if( (input != (FILE*) NULL) )
        {
          for(i=0; i<3; i++)
    	  {
    	  for(j=0; j<3; j++)
    	  {
    	  fullmatrix[i][j]=ReadFromFile(fullmatrix, input);
    	  }
    	  fscanf(input, "\n");
          }
        fclose(input);
    	printf("%f", fullmatrix[2][2]);
    	return(0);
    	}
     else
       {
        printf("*** Could not open input file, or there are not 9 elements in the matrix ***\n");
       }
    }  
    /*Write the external fucntion here, need a 2 dimensional array http://www.dfstermole.net/OAC/harray2.html*/
    
    int ReadFromFile(float fullmatrix[3][3], FILE *input)
    {
    float element; 
    fscanf(input,"%i,", &element);
    return (element);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well for starters, %i in I/O is for integers not floats.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Thanks a lot! that was the problem... it works now.

    I still have quite a bit to do on it so i may post again here in a few hours

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Okay i have a problem again, it does read the file: but it does it in a weird order.

    Matrix.dat contains:

    Code:
    1.001 -2.2000 3.1366
    5.8111 9.0003 -1.1064
    -3.3098 2.8243 4.5333
    My program looks like

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /*need to use an external function to computer det of a minor (2x2 matrix)*/
    /*Need to read from a file named matrix.dat*/
    
    /* Prototype your function here, also make sure you change all of these comments*/
    float ReadFromFile(float fullmatrix[3][3], FILE *input);
    float minordet(float minor[2][2]);
    
    int main(int argc, char* argv[])
    {
        FILE         *input;
        int           i, j;  
        float         fullmatrix[3][3];
        const char    inp_fn[]="matrix.dat";
    	
        /* Open files */
        input = fopen(inp_fn, "r");
        /* Check the pointers to files are not NULL, also check matrix has correct number of elements */
        if( (input != (FILE*) NULL) )
        {
          for(i=0; i<3; i++)
    	  {
    	  for(j=0; j<3; j++)
    	  {
    	  fullmatrix[i][j]=ReadFromFile(fullmatrix, input);
    	  }
    	  fscanf(input, "%f");
          }
        fclose(input);
    	printf("%f %f %f\n %f %f %f\n %f %f %f\n", fullmatrix[0][0], fullmatrix[0][1], fullmatrix[0][2], fullmatrix[1][0], fullmatrix[1][1], fullmatrix[1][2], fullmatrix[2][0], fullmatrix[2][1], fullmatrix[2][2]
    );
    	return(0);
    	}
     else
       {
        printf("*** Could not open input file, or there are not 9 elements in the matrix ***\n");
       }
    }  
    /*Write the external fucntion here, need a 2 dimensional array http://www.dfstermole.net/OAC/harray2.html*/
    
    float minordet(float minor[2][2])
    {
    float minordet;
    }
    
    float ReadFromFile(float fullmatrix[3][3], FILE *input)
    {
    float element; 
    fscanf(input,"%f,", &element);
    return (element);
    }
    and when it is run it shows

    Code:
    1.001 -2.2000 3.1366
    9.0003 -1.1064 -3.309800
    4.5333 4.5333 4.5333
    Any idea what the problem is?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Commas don't belong in format codes for scanf()/fscanf()/sscanf().

    Your function doesn't need the parameter of the array to read a number from a file - just a suggestion to remove it for cleaner code.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by Adak View Post
    Your function doesn't need the parameter of the array to read a number from a file.
    Sorry, which bit in the code are you referring to specifically here? Also, do you have any idea why the matrix that the program prints is not the same as the one that it is reading from? do i have a problem with the for loops?

    Thanks a lot

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    Commas don't belong in format codes for scanf()/fscanf()/sscanf().
    Likely this line. Tim S.
    Code:
    fscanf(input,"%f,", &element);
    Last edited by stahta01; 11-22-2010 at 12:34 PM. Reason: Wrong issue

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by stahta01 View Post
    Likely this line. Tim S.
    Code:
    fscanf(input,"%f,", &element);
    I have removed the comma from inside the quotation marks, yet the problem persists, could you point me in the right direction? Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from a 2d array
    By roaan in forum C Programming
    Replies: 2
    Last Post: 09-04-2009, 06:30 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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