在 C 中读取包含浮点数的 .txt 文本文件,以空格分隔 [英] Reading a .txt text file in C containing float, separated by space

查看:27
本文介绍了在 C 中读取包含浮点数的 .txt 文本文件,以空格分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

0.00000587898458 0.0014451541000 0.0000000000012450.00012454712235 0.1245465756945 0.012454712115140

... 640 列 480 行.

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

FILE* myfile=NULL;双 myvariable=0.0;myfile=fopen("myfile.txt","r");for(i =0, k=0 ; i

非常感谢您的帮助

解决方案

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

<块引用>

example.c:16: 警告:scanf 格式的宽度为零example.c:16: 警告:格式中的未知转换类型字符."

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

#include #定义高度2#定义宽度 3int main(void){文件 *我的文件;双myvariable;国际我;国际 j;myfile=fopen("myfile.txt", "r");for(i = 0; i <高度; i++){for (j = 0 ; j 

示例运行:

$ ./example0.000005878984580 0.001445154100000 0.0000000000012450.000124547122350 0.124546575694500 0.012454712115140

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

I have a txt file of this format

0.00000587898458 0.0014451541000 0.000000000001245
0.00012454712235 0.1245465756945 0.012454712115140

... with 640 columns and 480 lines.

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.

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. You'll want to use &myvariable there, 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, gcc warns me with your format string:

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:

#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 

这篇关于在 C 中读取包含浮点数的 .txt 文本文件,以空格分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆