简体   繁体   中英

Not reading data from file that should be there C++

so, in an effort to deal with 3D bool arrays greater then the total RAM memory of my computer, i tried translating them into file form the name of the file is the x coordinate and inside the file its written the y coordinate, the z coordinate and a 0 or 1 to indicate the bool value (I didnt put it all into one file cuz it was taking way too long to be read) Here is the code that makes the files

char fileName[100];
    for(double px = minX; px <= maxX; px = px + deltaX)
    {
        sprintf(fileName,"%lf.txt",px);
        allPoints = fopen(fileName,"w");
        for(double py = minY; py <= maxY; py = py + deltaY)
        {
            for(double pz = minZ; pz <= maxZ; pz = pz + deltaZ)
            {
                if(test(x,y,z))
                {
                    fprintf(allPoints,"%lf\t%lf\t1\n",py,pz);
                }
                else
                {
                    fprintf(allPoints,"%lf\t%lf\t0\n",py,pz);
                }
            }
        }
        fclose(allPoints);
    }

(the deltaX deltaY and deltaZ are very small, so there are a lot of points) All works well here, the files are created and everything the problem is when I try to read the files, here is the code i'm using

for(double kx = minX; kx <= maxX; kx = kx + deltaX)
    {
        for(double ky = minY; ky <= maxY; ky = ky + deltaY)
        {
            for(double kz = minZ; kz <= maxZ; kz = kz + deltaZ)
            {
                if(Check_if_True(kx,ky,kz))
                {
                    //do something
                }
            }
        }
    }

bool Check_if_True(double kx, double ky, double kz)
{
char ignore;
char fileN[256];
double iy;
double iz;
int b;

    sprintf(fileN,"%lf.txt",kx);
    ifstream reader (fileN);
    if(!reader.is_open())
    {
        printf("couldn't open file");
    }
    while(reader >> iy >> iz >> b)
    {
        if(iy == ky && iz == kz)
        {
            reader.close();
            if(b == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    printf("didn't find the values\n");
    reader.close();
    return false;
}

I could confirm that it does read the values correctly, by printing them in the screen, but it always prints out the "didn't find the values", as if it had scanned the whole file and didn't find the values required I've been stuck on this for three days now, I've tried with fstream and stdio, no difference... And yes, I need to be able to write and then read the file later on. Please help!

The problem is probably this line: if(iy == ky && iz == kz)

Here you are comparing floating point numbers, which are binary numbers with a certain number of bits in computers. They can't exactly represent same real numbers, as you can exactly represent with decimal numbers (and vice versa). In addition, when you do any math with floats, there are rounding errors. Therefore you usually don't want to compare them for exact equality, you just want to see if they are close enough to be considered the same value.

So do the above if like this:

#include <cmath>

const double EPSILON = 0.0000001; // choose appropriate value for your application

...

if(std::fabs(iy - ky) < EPSILON && std::fabs(iz - kz) < EPSILON) 

Note that above is not perfect, sometimes it's outright bad. Selecting good epsilon can be hard, even impossible in some applications. So, some reading, where you'll find some discussion on how to do comparisons, as well as link to a more math-heavy resources: What Every Programmer Should Know About Floating-Point Arithmetic or Why don't my numbers add up?

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