编写/读取文件时出错

发布于 2025-02-10 01:40:18 字数 2014 浏览 0 评论 0原文

我有2个功能与同一文件一起使用。第一个函数从文件中读取:

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

secnd函数在同一文件中写入:

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

它们都在循环中调用:

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

第一个函数从文件中读取变量值,然后程序对其进行某些操作,然后此变量值写入文件第二在同一位置i,j,k的功能。问题发生在循环的第二步。阅读功能返回“错误发生在阅读时间!”。无需打电话循环第二函数写作 - 一切都很好。也许我没有在第二个功能中释放一些资源?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文