1

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);
}
xxx
  • 11
  • 2
  • 3
    Welcome to Stack Overflow! Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh Dec 02 '16 at 15:50
  • 1
    Check the return value of `fgets(dat1, n, file1)` before using `dat1` – chux - Reinstate Monica Dec 02 '16 at 16:03
  • Depends on what's in your file I supposed, but this most likely is a memory leak. How many times do you loop through the `while` loop? Only a pointer to the last `malloc` is saved. – yano Dec 02 '16 at 16:07
  • 1
    1) `for(i = 0; i < n; i++) { sscanf(dat1, "%f", &m[i]); //getting error here }` repeatedly scans the same string `dat1`. Each iteration would be expected to have the same result. 2) Without posting what `fract16` is, how can we be certain it matches a `"%f"` (`double`)? 3) `/getting error here` --> What is that error? – chux - Reinstate Monica Dec 02 '16 at 16:08
  • Why, exactly, are the spaces before every line necessary? All they do is prevent us from copy-pasting the code to compile. – MD XF Dec 02 '16 at 16:09
  • Oh, but the code is incomplete anyway. Never mind. – MD XF Dec 02 '16 at 16:09
  • This is not causing that error, but wrong: `printf("%lf\n", m);`. It should be `printf("%p\n", m);` – MayurK Dec 02 '16 at 16:19
  • `printf` expects any pointer it's printing to be `void`.: --> `printf("%p\n", (void*)m);` – yano Dec 02 '16 at 16:22
  • regarding these lines: `if (file1 == NULL) { printf("I couldn't open 0.dat for reading.\n"); exit(0);`. 1) error messages should be output to `stderr`, not `stdout`. 2) when a system function returns an error indication, should output the system message that shows what the system thinks is the root of the problem. -- 1 and 2 can be handled by calling `perror()` similar to: `perror( "fopen for read for o.dat failed: ); 3) a returned value of 0 indicates the function was successful. Strongly suggest using: `exit( EXIT_FAILURE );` – user3629249 Dec 04 '16 at 07:23
  • 1
    never use the function: `feor()` to control a loop. In this case suggest using the call to `fgets()` as in:`while( fgets( dat1, n, file1 )` – user3629249 Dec 04 '16 at 07:26
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter value) to assure the operation was successful. – user3629249 Dec 04 '16 at 07:53
  • when calling any of the heap memory allocation functions (malloc, calloc, realloc), always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Dec 04 '16 at 07:54
  • for each call to `malloc()` there needs to be a call to `free() with the pointer returned from `malloc()` – user3629249 Dec 04 '16 at 07:55

2 Answers2

1

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

duckvader
  • 71
  • 13
0

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, ...

user3629249
  • 16,402
  • 1
  • 16
  • 17