编写/读取文件时出错
我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论