【问题标题】:How to read a text file that has float numbers to a float array in C如何将具有浮点数的文本文件读取到C中的浮点数组
【发布时间】:2016-10-12 05:05:47
【问题描述】:

我一直在尝试读取包含以下内容的文本文件:

0.000   1.800   3.240   4.374   5.249   5.905   6.377   7.696   6.887   6.974   6.974   6.904   6.778   6.609   6.406   6.177   5.930   5.670   5.403   5.133   4.863   4.596   4.333   5.077   3.829   4.589   3.360   3.140   2.931   2.732   2.543   2.365   2.198   2.040   1.891   1.752   1.622   1.500   1.387   1.281   1.182   1.091   2.006   0.927   0.853   0.786   0.723   0.665   0.611   0.561   0.515   0.473   0.434   0.398   0.365   0.335   0.307   0.281   0.257   0.236   0.216   1.197   0.180   0.165   0.151   0.138   0.126   0.115   0.105   0.096   0.088   0.080   0.073   0.067   0.061   1.055   0.051   0.046   0.042   0.038   0.035   0.032   0.029   0.026   0.024   0.022   0.020   0.018   0.017   0.015   0.014   0.012   0.011   0.010   0.009   0.009   0.008   0.007   1.006   0.006   0.005   0.005

这些数字在文本文件中的格式如上。

我的代码如下

#include <stdio.h> 

#include <stdlib.h>

void main()

{

        FILE *file = fopen("Data.txt", "r");
        float integers[102];

        int i=0;
        int num;
        while(fscanf(file, "%f", &num) >0) {
            integers[i] = num;
            i++;
        }
        if( file == NULL )
         {
            printf("Error while opening the file.\n");

         }
        fclose(file);
        for(i = 0;i <102; i++){
        printf("integers[%d] = %f",i, integers[i]);
        }


}

printf 的输出是整数中的所有元素在运行时都为零。此外,在尝试调试时,我注意到编译器跳过了 while 循环。我正在使用 Code Composer Studios。

【问题讨论】:

  • 为什么将浮点数数组称为“整数”?为什么将读取的整数保存在int,而不是float?为什么很晚才查看null
  • 开始测试fopen是否返回NULL,如果是,则打印错误消息,不要继续。
  • @Zeta 代码不是原创的,我不得不修改它以使其读取浮动。变量来自原始变量,没有更改。
  • @Zeta 我也想看看 while 循环是否读取了文件,所以我现在才注意到为什么你说得很晚。
  • 您在尝试读取文件后显示“无法打开文件”错误消息。这不是你应该做的。

标签: c arrays file text scanf


【解决方案1】:

以这种方法为例:

#include <stdio.h>
#include <stdlib.h>

int main() {
  FILE * fp;
  float fval[102];
  int n, i;

  fp = fopen("foo.txt", "w+");
  if (fp == NULL) {
    printf("failed to open file\n");
    return 1;
  }

  fputs("0.000   1.800   3.240   4.374   5.249   5.905   6.377   7.696 ", fp);
  rewind(fp);

  n = 0;
  while (fscanf(fp, "%f", &fval[n++]) != EOF)
    ;

  /* n-1 float values were successfully read */
  for (i=0; i<n-1; i++)
    printf("fval[%d]=%f\n", i, fval[i]);

  fclose(fp);
  return 0;
}

我得到这个输出:

fval[0]=0.000000
fval[1]=1.800000
fval[2]=3.240000
fval[3]=4.374000
fval[4]=5.249000
fval[5]=5.905000
fval[6]=6.377000
fval[7]=7.696000

【讨论】:

  • 我做了你的代码。没有看到数据文本文件所在的 foo 文本文件(顺便说一句,数据来自 matlab),搜索它。发现它在不同的文件中。该文件与数据文本文件处于同一级别。将数据文本文件移动到 foo 文本文件所在的位置。修复了问题。你先生是救生员。非常感谢
  • 我刚刚修改了代码,使其更简洁,更接近您对数组所做的操作。很高兴它有帮助!
【解决方案2】:

想不通

    //int num;
    float num; // should be float isn't is
    while(fscanf(file, "%f", &num) >0) {

更多

int main() // int main instead of void
{
    /* All declarations at one place */
    FILE *file = NULL; 
    float integers[102], num ;
    int i = 0;
    /* Try Open and exit if there is problem */ 
    file = fopen("Data.txt", "r");
    if( file == NULL ) {
        printf("Error while opening the file.\n");
        return -1;
    }
    /* Read and store EOF is always better */
    while( fscanf(file, "%f", &num) != EOF ) {
        integers[i++] = num;
    }
    /* Done with File Close it */
    fclose(file);
    /* Final Prints */
    for( i = 0; i < 102; i++ ){
        printf("integers[%d] = %.02f\n",i, integers[i]);
    }
    /* All success at this point */
    return 0;
}

【讨论】:

  • 感谢您指出这一点。修复了问题,仍然存在每个元素为零的数组的问题。
  • @OscarTovar 我回滚了您的问题编辑。您不应以使答案无效的方式编辑问题。
  • @hyde 抱歉不知道。顺便说一句,您如何关闭此线程,问题已解决。
【解决方案3】:

使用向量我们可以解决这个问题。参见下面的程序。这里 myValues 是二维数组。

ifstream 输入("Data.txt"); 向量 myValues;

for (float num; 输入 >> num; ) myValues.push_back(num);

ifstream 输入("my-file.txt"); std::vector myValues;

for (float f; 输入 >> f; ) myValues.push_back(f);

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2021-10-13
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多