简体   繁体   English

从C中的文件读取的数据开头的大量数字

[英]Large number at the beginning of data read from a file in C

I have a code snippet, where I want to read columnar data from a txt file. 我有一个代码段,我想从其中读取txt文件中的列数据。 However, whenever I do so, a large number - namely - -1.073742e+008 gets appended to the beginning of the file. 但是,每当我这样做时,--1.073742e + 008都会被附加到文件的开头。 I do not know where it is coming from or how to get rid of it. 我不知道它来自哪里或如何摆脱它。 Since this snippet is part of a larger program that is meant to read the file automatically and pass it to another application, manual deletion is not an option. 由于此代码段是一个较大的程序的一部分,该程序旨在自动读取文件并将其传递给另一个应用程序,因此不能选择手动删除。 My code as it currently stands is as follows. 我目前的代码如下。

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

void main()
{
    FILE * fout;
    int n;
    float f1;
    float f2;
    float f3;
    float npx[5208];
    float npy[5208];
    float npz[5208];
    int v1;
    int i;
/*read node cordinates fron the file */
    /*char buffer[100];*/
    fout = fopen("z1_115k.out", "r");
    /*fgets(buffer,100,fout);*/ /* skip the first line*/
    while(feof(fout)==0)
    {
        fscanf(fout, "%d %e %e %e\n", &v1, &f1, &f2, &f3);
        npx[v1]=f1;
        npy[v1]=f2;
        npz[v1]=f3;
    }
    fclose(fout); 
    fout = fopen("writeout9.txt" , "w");
    for(i=0;i<5208;i++)
    {
        fprintf(fout, "%e",npy[i]);
        fprintf(fout, "\n");
    }
    fclose(fout);
    getch();
}

The file I'm trying to read looks like this 我正在尝试读取的文件如下所示

         1 -1.998999214E-04 -6.326398761E-06  7.987323916E-04 
         2 -1.993482729E-04  1.613270797E-05  8.020100649E-04 
         3 -1.998304651E-04  8.233274457E-06  7.735857507E-04 
         4 -9.247181879E-05  1.772655523E-04  6.779084215E-04 
         5 -7.928538980E-05  1.833699498E-04  6.915458362E-04 
         6 -9.789415344E-05  1.918512862E-04  6.868232158E-04 
         7 -1.943270909E-04 -4.729676220E-05  8.172317175E-04 
         8 -1.892633736E-04 -6.464808394E-05  8.175745024E-04 

And the output I get for the first column is the following 我在第一列中得到的输出如下

-1.073742e+008
-1.998999e-004
-1.993483e-004
-1.998305e-004
-9.247182e-005
-7.928539e-005
-9.789415e-005
-1.943271e-004
-1.892634e-004

Why am I getting the -1.073e+08 value at the beginning? 为什么我一开始会得到-1.073e + 08的值?

You are writing into the arrays using the index given in the leftmost column in the file. 您正在使用文件最左列中给出的索引写入数组。 Since this starts with 1 in the first line of the file, entry #0 will remain uninitialized. 由于此文件的第一行以1开头,因此条目#0将保持未初始化状态。 This results in the garbage value you see when printing npy[0] . 这将导致您在打印npy[0]时看到的垃圾值。

Also, unrelated to this issue, you should read Why is “while ( !feof (file) )” always wrong? 另外,与此问题无关,您应该阅读为什么“ while(!feof(file))”总是错误的? This may cause errors at the end of the file, and incorrect handling of bad file format. 这可能会导致文件末尾的错误,以及对错误文件格式的不正确处理。 Your loop should look like: 您的循环应如下所示:

while (fscanf(fout, "%d %e %e %e\n", &v1, &f1, &f2, &f3) == 4) ...

And lastly, you probably also want to check that the array index is in range before writing to it. 最后,您可能还想在写入数组索引之前检查它是否在范围内。

Your file starts with the number 1, so you access npx[1] . 您的文件以数字1开头,因此您访问npx[1] Your printing loop starts with 0 and accesses npx[0] , which has no value set. 您的打印循环从0开始并访问npx[0] ,该值未设置值。

Actual problem is likely: You store values to your arrays from from index 1 , but print from index 0 . 可能出现实际问题:您从索引1将值存储到数组,但从索引0打印。 As the arrays are not initialized, you get whatever is stored in the storage cells. 由于未初始化数组,因此可以获取存储在存储单元中的任何内容。 You can either subtract 1 from the index read (recommended, but read on first ). 您可以从读取的索引中减去1 (推荐,但首先读取 )。 Or you print the values from index 1 . 或者,您从索引1打印值。 Not recommended as you wast the first entry of the arrays and this behaviour is uncommen in C and all other languages with 0 as the first index. 不建议这样做,因为您不是数组的第一个条目,并且这种行为在C和所有其他以0作为第一个索引的语言中并不常见。

But there are many other issues with your code: 但是您的代码还有许多其他问题:

  1. The signature of main is not compliant to the standard. main的签名不符合该标准。 This invokes undefined behaviour on return. 这将在返回时调用未定义的行为 Actually your compiler should warn. 实际上,您的编译器应发出警告。
  2. feof only checks if the end-of-file indicator is set from a previous function . feof仅检查是否从上一个函数设置文件结束指示符。 It does not check if the file is at the end itself! 检查,如果该文件是在结束自己!
  3. Always check the results of functions which can return an error condition. 始终检查可能返回错误情况的函数结果。 fopen is such a function, scanf another! fopen是这样的功能, scanf另一个! Not testing might invoke undefined behaviour . 不进行测试可能会引起未定义的行为
  4. Check the array index v1 before using! 使用前检查数组索引v1 That is from an unreliable source (even more as you do nocht check the result of fscanf ) and any out-of-bounds access will invoke undefined behaviour . 那是来自一个不可靠的来源(甚至在您检查fscanf的结果时,甚至更多),任何越界访问都将调用未定义的行为
  5. (more of a comment than an issue) getch is non-standard. (评论多于问题) getch是非标准的。

Always enable compiler warnings and pay heed to them! 始终启用编译器警告并留意它们!

Note: during testing, it is a good idea to instantly print the values read in the read-loop in the read-loop. 注意:在测试过程中,最好立即在读取循环中打印在读取循环中读取的值。

The first column is the array index, which means it would start from zero, not one. 第一列是数组索引,这意味着它将从零开始,而不是一个。

You could change to 您可以更改为

while(feof(fout)==0)
{
    fscanf(fout, "%d %e %e %e\n", &v1, &f1, &f2, &f3);
    npx[v1-1]=f1;
    npy[v1-1]=f2;
    npz[v1-1]=f3;
}

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

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