简体   繁体   中英

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. However, whenever I do so, a large number - namely - -1.073742e+008 gets appended to the beginning of the file. 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?

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. This results in the garbage value you see when printing npy[0] .

Also, unrelated to this issue, you should read Why is “while ( !feof (file) )” always wrong? 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] . Your printing loop starts with 0 and accesses npx[0] , which has no value set.

Actual problem is likely: You store values to your arrays from from index 1 , but print from index 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 ). Or you print the values from index 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.

But there are many other issues with your code:

  1. The signature of main is not compliant to the standard. 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 . 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! Not testing might invoke undefined behaviour .
  4. Check the array index v1 before using! 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 .
  5. (more of a comment than an issue) getch is non-standard.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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