Thread: Reading txt file to a MATRIX

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Reading txt file to a MATRIX

    Hey guys
    I'm writing a programme to generate a random matrix, print it to a file and then read from the file and print to the screen. I'm having some issues with reading from the file. My programme reads in the number of rows and columns in my matrix fine but I can't seem to get it to read in the data values properly. I'm completely stuck so any help at all would be very appreciated!

    Here's the structure of my matrix:

    Code:
    typedef struct matrep {
              unsigned rows, columns;
              double *data;
              }MATRIX;
    Here's my write_matrix function (prints to a txt file)

    Code:
    void matrix_write(FILE *filep, MATRIX A)
    {
         int i, j;
         double *ptr;
         
         ptr=A.data;
         
         if((filep = fopen("matrixA.txt","w"))==NULL)
         {
                  printf("\nFailed to Open File.\n");
                  exit(1);
                  }
                  
         fprintf(filep, "rows = %d, columns = %d\n", A.rows, A.columns);         
         for(i=0; i<A.rows; i++)
         {
           fprintf(filep, "\n");
           for(j=0; j<A.columns; j++)
           {
             fprintf(filep, "  %5.2lf", *ptr++);
                    }
                  }
             fclose(filep);
             free(ptr);
         }
    and here's my read_matrix file (attempts to read the matrix from the txt file)

    Code:
    MATRIX matrix_read(char file_name[15])
    {
      int i,j, m, n;
      MATRIX B;
      FILE *filep;
      double *ptr = NULL;
      double x;
      
      if((filep = fopen("matrixA.txt", "r"))==NULL)
      {
       printf("\nFailed to open File.\n");
                }
       if(fscanf(filep, "rows = %u, columns = %u\n", &m, &n) != 2)
        {
            printf( "Failed to read dimensions\n");
            n = 0;
            m = 0;
        }
       B.data = malloc(sizeof(double)*m *n);
       if(B.data ==0)
           {
            printf("Failed to allocate memory");
            exit(1);
            }
            
       fscanf(filep,"rows = %u, columns = %u\n",&m,&n);
      
       ptr = B.data;
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                
                if (fscanf(filep, "  %5.2lf", &x) != 1)
               {
                   printf("Failed to read element [ %d,%d ]\n", i, j);
                   B.data=0;
                }
                printf("%5.2lf\t", x);
                *ptr++ = x; 
                }
              }
       
       B.rows=m;
       B.columns=n;
       return B;
       fclose(filep);
       free(ptr);
       }
    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Check the return value of scanf every time. For example, in your output routine you output the line "rows = %d, columns = %d\n" once, but in your input routine you try to scanf it twice. Also it would be good to check the return value of fprintf since you are outputting to a file, which is not guaranteed to work (not enough disk space, broken network connection, removed medium, etc...).

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void matrix_write(FILE *filep, MATRIX A)
    {
    ...
    free(ptr);
    }
    Why do you want to free "ptr"? You haven't allocated space for it and it will point to the memory after the matrix at the end of the loop.

    Code:
    MATRIX matrix_read(char file_name[15])
    {
    ...
    return B;
    fclose(filep);
    free(ptr);
    }
    The statements after the return won't be executed, thus you don't close the file (freeing "ptr" is as wrong as in your output function).

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a matrix and printing out the matrix
    By Lina_inverse in forum C Programming
    Replies: 9
    Last Post: 10-23-2012, 04:09 PM
  2. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2011, 07:46 AM
  3. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2010, 09:45 AM
  4. Miximum matrix size? / reading big arrays of data
    By Elya in forum C Programming
    Replies: 16
    Last Post: 07-08-2009, 07:43 AM
  5. Reading char matrix from file
    By TriKri in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 02:05 AM

Tags for this Thread