将大型结构输出到HD

时间:2010-08-21 06:44:29

标签: visual-c++ structure

我有一个大型结构的数组,我试图输出到硬盘驱动器。我似乎能够很好地写入硬盘驱动器(尽管通过查看二进制数据很难验证),但是当我尝试将其读回时,我总是会出现乱码。我有什么想法吗?

这是结构配置:

class xyz
{
public:
    double x, y, z;
};
class trianglePackage
{
public:

    int score;
    int position;

    xyz contactCoordinates;
    xyz normalVector;
    xyz locatorOffset;

};
class quadanglesOutput
{
public:

    int locator1position, locator2position, locator3position, locator4position;

    xyz centroid;

    int surfaceAreaScore;
    int centroidDifferance1Score;
    int centroidDifferance2Score;
    int minDistance1Score;
    int minDistance2Score;

    int totalLocatorScore;
    int totalHullScore;
    int totalScore;

    double surfaceArea;
    double centroidDifferance1;
    double centroidDifferance2;
    double minDistance1;
    double minDistance2;

    int hull;

    trianglePackage locator1, locator2, locator3, locator4;
};

以下是我正在使用的读/写函数:

void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{

    string outputName = parameters.fileName + " " + description + ".bin";
    cout << "Output " << outputName.c_str() << "...";
    ofstream output2;
    output2.open(outputName.c_str());
    output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
    output2.close();
    cout << "done" << endl;

}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
    string fileName = parameters.fileName + " " + description + ".bin";
    cout << "openining " << fileName << "...";
    ifstream readFile;
    readFile.open(fileName.c_str());
    readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
    readFile.close();
    cout << "done" << endl;
}

通常,结构数组的长度大约为100,但通常只有大约前25个读回正确,其他所有内容都是默认的未初始化数据。

我99%确定我的代码有问题,但有可能它与四字节对齐有关吗?

感谢。

1 个答案:

答案 0 :(得分:1)

字节对齐可能是一个问题,使用pragma。 尝试使用

包装类
#PRAGMA PACK PUSH(1)
....
#PRAGMA PACK POP

#PRAGMA PACK(1)
struct{
..
}

尝试这些:
强制流的二进制标志。

ios_base :: binary

readFile.open(fileName.c_str(), ios_base::binary);

尝试冲洗流。

stream.write(...)
stream.flush()

//我知道close()应该冲洗它。

<强>更新
一切都适合我:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;
#pragma pack(1)
class xyz
{
public:
    double x, y, z;
};

#pragma pack(1)
class trianglePackage
{
public:

    int score;
    int position;

    xyz contactCoordinates;
    xyz normalVector;
    xyz locatorOffset;

};

#pragma pack(1)
class quadanglesOutput
{
public:

    int locator1position, locator2position, locator3position, locator4position;

    xyz centroid;

    int surfaceAreaScore;
    int centroidDifferance1Score;
    int centroidDifferance2Score;
    int minDistance1Score;
    int minDistance2Score;

    int totalLocatorScore;
    int totalHullScore;
    int totalScore;

    double surfaceArea;
    double centroidDifferance1;
    double centroidDifferance2;
    double minDistance1;
    double minDistance2;

    int hull;

    trianglePackage locator1, locator2, locator3, locator4;
};

class param
{
public:
    string fileName;
    int topXlist;
};


void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{

    string outputName = parameters.fileName + " " + description + ".bin";
    cout << "Output " << outputName.c_str() << "...";
    ofstream output2;
    output2.open(outputName.c_str());
    output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
    output2.close();
    cout << "done" << endl;

}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
    string fileName = parameters.fileName + " " + description + ".bin";
    cout << "openining " << fileName << "...";
    ifstream readFile;
    readFile.open(fileName.c_str());
    readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
    readFile.close();
    cout << "done" << endl;
}



int main(int argc, char *argv[])
{

    quadanglesOutput a = {0};
    cout<<"total score:"<<a.totalScore<<endl;
    cout<<"locator position:"<<a.totalScore<<endl;
    cout<<"locator position:"<<a.locator1.position<<endl;
    cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
    cout<<"sizeof quadangsomething:"<<sizeof(quadanglesOutput)<<endl;
    a.totalScore=1;
    a.locator1.position=333445;
    a.locator2.normalVector.y = 999.3224;
    cout<<"total score:"<<a.totalScore<<endl;
    cout<<"locator position:"<<a.locator1.position<<endl;
    cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
    param p = {"C:/", 1};
    outputQuadangleOutput(&a, "file1", p);

    quadanglesOutput *b = new quadanglesOutput();
    readIn(b, p, "file1");
    cout<<"new total score:"<<b->totalScore<<endl;
    cout<<"new locator position:"<<b->locator1.position<<endl;
    cout<<"new locator position:"<<b->locator2.normalVector.y <<endl;

    delete b;





    string asdf;
    cin>>asdf;

};

<强>输出:
总分:0
定位器位置:0
locator2.normalVector.y:0
sizeof quadangsomething:436
总分:1
定位器位置:333445
locator2.normalVector.y:999.322
输出C:/ file1.bin ...完成
openining C:/ file1.bin ...完成
新总分:1
新定位器位置:333445
new locator2.normalVector.y:999.322

没有编译指示它仍然是正确的,但你可以看到大小的差异:

  

sizeof quadangsomething:440

但是通过网络发送结构时包装它是很好的 因为这里系统总是以相同的方式分配它。