2010-08-21 22 views
0

我有一个大型结构阵列,我试图输出到硬盘驱动器。我似乎能够写入硬盘驱动器(虽然很难通过查看二进制数据来验证),但是当我尝试读取它时,总是会出现乱码。任何想法我做错了什么?输出大型结构到HD

这里的结构配置:

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

这可能是字节对齐的问题,请使用pragma。 尝试包装类各地的

#PRAGMA PACK PUSH(1) 
.... 
#PRAGMA PACK POP 

#PRAGMA PACK(1) 
struct{ 
.. 
} 

尝试那些还有:为流
力二进制标志。

的ios_base ::二进制

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

尝试刷新流。

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

//我知道close()应该刷新它。

UPDATE:
一切工作对我来说:

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

}; 

OUTPUT:
总比分: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

但打包发送结构上网络时,这是件好事。
因为这里的系统始终以相同的方式对其进行排列。

+0

嗯,它仍然在做和以前一样的事情。前25个阅读条目都很好,其余都是乱码。 – Faken 2010-08-21 07:28:00

+0

二进制标志似乎甚至减少了正确读取值的数量,现在它只是在产生看似随机的输出之前读取前10个元素(而不是先前以十六进制代码输出“CD”)。 – Faken 2010-08-21 07:38:24

+1

@faken:您是否使用二进制模式进行输入和输出? – 2010-08-21 10:48:26