【问题标题】:Unable to retain array outside the while loop无法在 while 循环之外保留数组
【发布时间】:2016-12-02 15:42:51
【问题描述】:

我目前正在编写下面的 C 代码。我需要在fclose 之后访问while 循环之外的数组。 blackfin ADSP 内核似乎每次运行时都会崩溃。我将需要它进一步执行 FFT。请帮忙!

#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;
}

好的,谢谢大家纠正我的错误,但问题仍然没有解决。我能够打印内部的所有值,但在循环之外它只打印数据集的最后一个值,是否有任何精确的解决方案?我用谷歌搜索了几个小时,但还没有成功。 代码如下>

#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);
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!请看Why is “while ( !feof (file) )” always wrong?
  • 在使用dat1之前检查fgets(dat1, n, file1)的返回值
  • 取决于我认为的文件中的内容,但这很可能是内存泄漏。你循环了多少次while 循环?只保存指向最后一个malloc 的指针。
  • 1) for(i = 0; i &lt; n; i++) { sscanf(dat1, "%f", &amp;m[i]); //getting error here } 重复扫描相同的字符串 dat1。每次迭代都应该有相同的结果。 2) 如果不发布fract16 是什么,我们如何确定它与"%f" (double) 匹配? 3) /getting error here --> 这是什么错误?
  • 永远不要使用函数:feor() 来控制循环。在这种情况下,建议使用对fgets() 的调用,如:while( fgets( dat1, n, file1 )

标签: c embedded signal-processing blackfin


【解决方案1】:

您可以在读取文件之前在 while 循环之外为缓冲区分配内存。然后每次读入缓冲区之前,只需使用 memset 并将缓冲区设置为所有空字符。

另外,尝试使用 fread 直接读取缓冲区而不是 fgets

【讨论】:

    【解决方案2】:

    变量m被定义为fract16的指针和数组

    解决问题建议:

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

    这个错误是因为m已经是一个指针,你希望它继续是一个指针

    顺便说一句。该代码没有检查在对fgets() 的调用中实际读取了多少数据,因此for() 很可能在实际数据结束后读取。并且每次通过while() 循环都会破坏/覆盖从先前调用malloc() 获得的指针

    然后在代码后面是语句:

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

    但是m 是一个指向 `fract16 对象数组的指针。

    而那些fract16 对象可能是double 值,但该细节尚不清楚。在任何情况下,对printf() 的调用最多只会从输入文件的最后一行的开头输出一个双精度值。那是你真正想做的吗?

    注意:dat1[] 被声明为long int 的数组,但对sscanf() 的调用似乎试图提取float 值。

    I.E.代码在数据类型、单个值的提取和打印方面都不一致。

    需要注意的一点:在当前代码中,由于指针mmalloc() 的调用反复覆盖,并且由于使用feof(),最后一次调用@ 987654341@ 将失败,因此dat1[] 的内容将以 NUL 字节开头

    建议分配一个指向fractl16对象的指针数组

    然后对于读取的每一行,使用malloc()设置指针数组中的下一个指针,...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多