Andrey Angerchik
Andrey Angerchik

Reputation: 51

Errors when writing/reading a file

I have 2 functions working with the same file. The first function reads from a file:

myRepositoryPointCloud::point3d myRepositoryPointCloud::Reading(int i, int j, int k)
{
  ifstream rf("C:\\PointCloud.dat", ios::out | ios::binary);
  if (!rf) {
      cout << "Cannot open file!" << endl;
  }

  point3d resultPt;
  rf.seekg((i * GetCntPointY() * GetCntPointZ() + j * GetCntPointZ() + k) * sizeof(point3d));

  rf.read((char *)&resultPt, sizeof(point3d));

  rf.close();
  if (!rf.good()) {
      cout << "Error occurred at reading time!" << endl;
  }
  return resultPt;
}

The secnd function write in the same file:

void myRepositoryPointCloud::Writing(int i, int j, int k, point3d & APoint)
{
  ofstream wf("C:\\PointCloud.dat", ios::out | ios::binary);

  if (!wf) {
      cout << "Cannot open file!" << endl;
  }

  wf.seekp((i * GetCntPointY() * GetCntPointZ() + j * GetCntPointZ() + k) * sizeof(point3d));

  wf.write((char *)&APoint, sizeof(point3d));

  wf.close();
  if (!wf.good()) {
      cout << "Error occurred at writing time!" << endl;
  }
}

They are both called in a loop:

      point3d CurrentPoint;

      for (int i = 0; i < EndPoint.i - StartPoint.i; i++)
        {
          for (int j = 0; j < EndPoint.j - StartPoint.j; j++)
            {
              for (int k = 0; k < EndPoint.k - StartPoint.k; k++)
                {
                  CurrentPoint = FPointCloud->Reading(i, j, k);

                  if (!CurrentPoint.deleted) 
                  {
                    Sphere->CalcIntersectSphereToPoint(&CurrentPoint);                    

                    if (CurrentPoint.deleted || CurrentPoint.visible)
                    {
                       FPointCloud->Writing(i, j, k, &CurrentPoint);
                    }
                  }
            }
        }
    }

First function reading variable value from file, then program do some things with them, and then this variable value write to file second function at the same position i, j, k. Problem occured at the second step of loop. The reading function return "Error occurred at reading time!". Without calling in loop second function Writing - all ok. Maybe I'm not freeing some resources in the second function?

Upvotes: 0

Views: 212

Answers (0)

Related Questions