2010-09-03 57 views
2

我最近做了一個編程測試,其中有一個我無法解決的ifstream部分。從那以後,我一直試圖在空閒時間解決這個問題而無濟於事。ifstream在編程測試中的幫助

問題基本上是從二進制文件中讀取並提取信息。

下面是文件格式:

 
------------------------------------------------------------------------------ 
| File Offset (in Bytes)| Value Type     | Value Description  | 
------------------------------------------------------------------------------ 
| 0      | Unsigned Integer (32 bits) | number of entities | 
------------------------------------------------------------------------------ 
| 4      | Entity information (see | Entity 0    | 
|      | below)      |      | 
------------------------------------------------------------------------------ 
| 4+32     | Entity Information   | Entity 1    | 
------------------------------------------------------------------------------ 
| ...     | ...      | ...     | 
------------------------------------------------------------------------------ 
| 4 + (32 * N)   | Entity Information   | Entity N    | 
------------------------------------------------------------------------------ 

Entity Information: 
------------------------------------------------------------------------------ 
| Offsett (in Bytes)| Value Type     | Value Description   | 
------------------------------------------------------------------------------ 
| 0     | Unsigned short (16 bits) | Unique ID     | 
------------------------------------------------------------------------------ 
| 2     | Unsigned short (16 bits) | Entity type ID   | 
------------------------------------------------------------------------------ 
| 4     | Single-precision float (32 | Position X coordinate  | 
|     | bits)      |       | 
------------------------------------------------------------------------------ 
| 8     | Single-precision float (32 | Position Y coordinate  | 
|     | bits)      |       | 
------------------------------------------------------------------------------ 
| 12    | Single-precision float (32 | Forward Normal X   | 
|     | bits)      | Component     | 
------------------------------------------------------------------------------ 
| 16    | Single-precision float (32 | Forward Normal Y   | 
|     | bits)      | Component     | 
------------------------------------------------------------------------------ 

這裏是我的代碼:

void readFile() 
{ 
    ifstream ifs ("binaryFile.bin" , ifstream::in); 

    while (ifs.good()) 
    { 
    int numEntities = 0; 
    ifs.read((char*)&(numEntities), sizeof(unsigned short)); 

    for(int i=0; i<numEntities; i++) 
    { 
     unsigned short uID; 
     unsigned short eID; 
     float x; 
     float y; 
     float forward_x; 
     float forward_y; 

     ifs.read((char*)&(uID), sizeof(unsigned short)); 
     ifs.read((char*)&(eID), sizeof(unsigned short)); 
     ifs.read((char*)&(x), sizeof(float)); 
     ifs.read((char*)&(y), sizeof(float)); 
     ifs.read((char*)&(forward_x), sizeof(float)); 
     ifs.read((char*)&(forward_y), sizeof(float)); 
    } 
    } 
    ifs.close(); 
} 

感謝。

+0

爲什麼不讓'numEntities'成爲'unsigned short'? – meagar 2010-09-03 21:01:15

回答

2

當你說你有問題時,你看到了什麼輸出,它與你期望看到的有什麼不同?

一些一般性建議:首先,如果您打算使用二進制數據讀取數據,請嘗試使用二進制模式下的數據流。

此外,您的第一個read操作將數據存儲到int,但讀取的長度爲unsigned short。這是故意的嗎?

當您將指針指向char*作爲read的第一個參數時,請嘗試使用明確的reinterpret_cast<char *>來代替。

+1

我只是爲了讓鄰居聽到它而努力。謝謝。 – dornad 2010-09-03 21:04:25

+0

如果你想確保你正在讀取每個項目的正確的字節數,使用略少出錯的語法'sizeof(numEntities)'而不是'sizeof(int)'。 – bta 2010-09-03 21:12:10

2

你正在對待numEntities像一個16位短(當根據規格和您的聲明)它應該是一個32位整數。您正在讀取兩個字節(sizeof(unsigned short))而不是4個;嘗試sizeof(int),並將其中的一些內容扔在那裏:

assert(sizeof(unsigned short) == 2); 
assert(sizeof(int) == 4); 
assert(sizeof(float) == 4);