C语言 在 C 中读取包含浮点数的 .txt 文本文件,以空格分隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7151922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:27:13  来源:igfitidea点击:

Reading a .txt text file in C containing float, separated by space

ctext

提问by thomsala

it looks common and obvious but I've already read txt file in C in the past and here I am really stuck.

它看起来很常见而且很明显,但我过去已经用 C 语言阅读过 txt 文件,在这里我真的被卡住了。

I have a txt file of this format

我有一个这种格式的txt文件

0.00000587898458 0.0014451541000 0.000000000001245
0.00012454712235 0.1245465756945 0.012454712115140

... with 640 columns and 480 lines.

... 640 列和 480 行。

I want to put each number of my txt file in a float with the maximum of precision as it is possible, and in a for loop.

我想尽可能将我的 txt 文件的每个数字放在一个具有最大精度的浮点数中,并放在一个 for 循环中。

FILE* myfile=NULL;
double myvariable=0.0;
myfile=fopen("myfile.txt","r");

for(i =0, k=0 ; i< height; i++)
    for (j=0 ; j< width ; j++){
fscanf(myfile,"%0.20f",&myvariable);
printf("%0.20f",myvariable);
k++;
}
}
fclose(myfile);

Thank you very much for your help

非常感谢您的帮助

采纳答案by Carl Norum

There are several errors in your program - mismatched braces, undefined variables, etc. The most important, however, and the one most likely to be causing your problem, is that you're not passing a pointer to myvariablein your fscanf()call. You'll want to use &myvariablethere, so that fscanf()can fill it in appropriately. You probably don't need the format string to be so complicated, either - "%lf"should work just fine to read a double. In fact, gccwarns me with your format string:

您的程序中有几个错误 - 不匹配的大括号、未定义的变量等。然而,最重要的也是最有可能导致问题的一个是您没有myvariablefscanf()调用中传递指针。你会想要在&myvariable那里使用,以便fscanf()可以适当地填写它。您可能也不需要如此复杂的格式字符串 -"%lf"应该可以正常读取double. 事实上,gcc用你的格式字符串警告我:

example.c:16: warning: zero width in scanf format
example.c:16: warning: unknown conversion type character ‘.' in format
example.c:16: warning: zero width in scanf format
example.c:16: warning: unknown conversion type character ‘.' in format

And then my output becomes just 0. Try "%lf". Here's a complete working example with your sample input:

然后我的输出变成了 0. 试试"%lf". 这是一个包含您的示例输入的完整工作示例:

#include <stdio.h>

#define HEIGHT 2
#define WIDTH  3

int main(void)
{
  FILE *myfile;
  double myvariable;
  int i;
  int j;

  myfile=fopen("myfile.txt", "r");

  for(i = 0; i < HEIGHT; i++)
  {
    for (j = 0 ; j < WIDTH; j++)
    {
      fscanf(myfile,"%lf",&myvariable);
      printf("%.15f ",myvariable);
    }
    printf("\n");
  }

  fclose(myfile);
}

Example run:

示例运行:

$ ./example 
0.000005878984580 0.001445154100000 0.000000000001245 
0.000124547122350 0.124546575694500 0.012454712115140 

回答by Hans Passant

 fscanf(myfile,"%0.20f",myvariable);

You have to pass a pointer, use &myvariable instead. Fix:

你必须传递一个指针,使用 &myvariable 代替。使固定:

 fscanf(myfile, "%lf", &myvariable);

回答by Keith Thompson

I can only guess, since you haven't shown us your actual source code, but ...

我只能猜测,因为您没有向我们展示您的实际源代码,但是......

fscanf's "%f"format expects a pointer to float; you're giving it a pointer to double. That's probably what's causing the problem you're seeing. Use "%lf"for double(or "%Lf"for long double).

fscanf"%f"格式需要一个指向float; 你给它一个指向double. 这可能是导致您看到的问题的原因。使用"%lf"double(或"%Lf"long double)。

Yes, this is an inconsistency between the *printfand *scanffunctions. It exists because floatarguments to *printfare promoted to double; there's no such promotion for pointers, such as the float*arguments you might pass to fscanf.

是的,这是*printf*scanf功能之间的不一致。它存在是因为 的float参数*printf被提升为double; 指针没有这样的提升,例如float*您可能传递给的参数fscanf

And just use "%lf", not "%0.20lf"; I'm not sure the latter is even valid.

只是使用"%lf",而不是"%0.20lf"; 我不确定后者是否有效。

回答by Happy Green Kid Naps

  1. You say you are stuck, but do not get into specifics regarding what exactly is the problem you are encountering.
  2. You most likely intended to pass the address of 'myvariable' to fscanf.
  3. Even if you don't care so much about precision (and in your case, you clearly say tjat you do), you should prefer double over float. You should choose float only when you have a very strong reason for preferring float over double, and even that, should be in very specific situations.
  4. Always check calls to fopen to ensure that it succeeded.
  1. 你说你被卡住了,但没有详细说明你遇到的问题究竟是什么。
  2. 您很可能打算将“myvariable”的地址传递给 fscanf。
  3. 即使您不太在意精度(在您的情况下,您清楚地说是 tjat),您也应该更喜欢 double 而不是 float。仅当您有非常强烈的理由更喜欢 float 而不是 double 时,您才应该选择 float,即使如此,也应该在非常特殊的情况下。
  4. 始终检查对 fopen 的调用以确保它成功。