Unable to retain array outside the while loop

264 Views Asked by At

I am currently working on the C code below. I need to access the array outside the while loop, after fclose. It appears that the blackfin ADSP kernel crashes every time I run it. I will need it further to perform FFT. Please help!

#include <stdlib.h>
#include <stdio.h>
#include <flt2fr.h>
#include <fract_math.h>
#include <math_bf.h>
#include <complex.h>
#include <filter.h>

int main() 
{
    int n = 1024;
    long int dat1[n];
    FILE *file1;
    fract16 *m;
    int i;

    // file1 open and read the values
    file1 = fopen("0.dat", "r");
    if (file1 == NULL) {
       printf("I couldn't open 0.dat for reading.\n");
       exit(0);
    }

    while (!feof(file1)) {
        fgets(dat1, n, file1);
        m = malloc(sizeof(fract16) * n);
        for (i = 0; i < n; i++) {
            sscanf(dat1, "%f", &m[i]); //getting error here
        }
    }

    fclose(file1);
    printf("%lf\n", m);
    return 0;
}

Alright, thank you all for correcting my mistakes, but the problem is still unresolved. I am able to print all of the values inside, but outside the loop it prints just the last value of the data set, is there any precise solution for this? I googled for hours but no success yet. The code is as follows >

#include <stdlib.h>
#include <stdio.h>
#include <flt2fr.h>
#include<fract_math.h>
#include <math_bf.h>
#include <complex.h>
#include <filter.h>
int main()
{
    int n = 1024;
    long int dat1[n];
    FILE *file1;
    fract16 *m;

    file1 = fopen("0.dat", "r");
      if (file1 == NULL) {
         printf("I couldn't open 0.dat for reading.\n");
         exit(0);
      }

    while( !feof(file1))
    {

       fgets(dat1,n,file1);
       sscanf(dat1, "%f", &m);
       printf("%f\n",m); //Prints all elements in the 1st column of the  array, 0.dat is a nx2 matrix
    }
    fclose(file1);
}
2

There are 2 best solutions below

0
On

You can allocate memory for the buffer before reading the file, outside the while loop. Then every time before reading into the buffer, simply use memset and set the buffer to all null characters.

Also, try using fread to read directly into the buffer rather than fgets

0
On

the variable m is defined as a pointer to and array of fract16

to fix the problem suggest:

if( 1 != sscanf(dat1, "%f", m+(sizeof(fract16)*i) )
{
    perror( "sscanf failed" );
    exit( EXIT_FAILURE );
}

The error is being cause because m is already a pointer and you want it to continue to be a pointer

As an aside. the code is not checking how much data was actually read in the call to fgets() so the for() may very well be reading behond the end of the actual data. And each trip through the while() loop is destroying/overlaying the pointer obtained from the prior call to malloc()

then later in the code is the statement:

printf("%lf\n", m);

But m is a pointer to an array of `fract16 objects.

and those fract16 objects might be double values, but that detail is not clear. In any case, this call to printf() will, at best, only output a single double value from the beginning of the last line in the input file. Is that what you really want to do?

Note: dat1[] is declared as an array of long int, but the call to sscanf() seems to be trying to extract float values.

I.E. the code is not consistent about the data types, nor the extraction of individual values, nor the printing.

One thing to note: with the current code there is a massive memory leak due to the pointer m being repeatedly overwritten by the calls to malloc() And due to the use of feof(), the last call to fgets() will fail, so the cotents of dat1[] will be starting with a NUL byte

Suggest allocating an array of pointers to fractl16 objects

Then for each line read, use malloc() to set the next pointer in the array of pointers, ...