简体   繁体   English

读取包含float的C中的.txt文本文件,用空格分隔

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

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 非常感谢您的帮助

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 myvariable in your fscanf() call. 你的程序中有几个错误 - 不匹配的大括号,未定义的变量等。然而,最重要的是,最可能导致你的问题的是,你没有在fscanf()调用中传递指向myvariable的指针。 You'll want to use &myvariable there, so that fscanf() can fill it in appropriately. 你需要在那里使用&myvariable ,这样fscanf()可以适当地填充它。 You probably don't need the format string to be so complicated, either - "%lf" should work just fine to read a double . 你可能不需要格式字符串这么复杂 - "%lf"应该可以很好地读取一个double In fact, gcc warns me with your format string: 实际上, gcc用你的格式字符串警告我:

 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" . 然后我的输出变为0.尝试"%lf" Here's a complete working example with your sample input: 以下是您的示例输入的完整工作示例:

#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 
 fscanf(myfile,"%0.20f",myvariable);

You have to pass a pointer, use &myvariable instead. 你必须传递一个指针,而不是使用&myvariable。 Fix: 固定:

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

I can only guess, since you haven't shown us your actual source code, but ... 我只能猜测,因为你还没有向我们展示你的实际源代码,但......

fscanf 's "%f" format expects a pointer to float ; fscanf"%f"格式要求指针float ; you're giving it a pointer to double . 你给它一个指向double的指针。 That's probably what's causing the problem you're seeing. 这可能是导致您遇到问题的原因。 Use "%lf" for double (or "%Lf" for long double ). 使用"%lf"表示double (或"%Lf"表示long double )。

Yes, this is an inconsistency between the *printf and *scanf functions. 是的,这是*printf*scanf函数之间的不一致。 It exists because float arguments to *printf are promoted to double ; 它存在是因为*printf float参数被提升为double ; there's no such promotion for pointers, such as the float* arguments you might pass to fscanf . 指针没有这样的提升,例如你可能传递给fscanffloat*参数。

And just use "%lf" , not "%0.20lf" ; 只需使用"%lf" ,而不是"%0.20lf" ; I'm not sure the latter is even valid. 我不确定后者是否有效。

  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. 你很可能打算将'myvariable'的地址传递给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. 即使你不太关心精确度(在你的情况下,你明确地说你做了tjat),你应该更喜欢双重浮动。 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. 始终检查fopen的调用以确保它成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM