【发布时间】:2013-12-27 21:40:42
【问题描述】:
我正在尝试使用“.hgt”格式文件中的二进制数据的应用程序。我在 C++ 中找到了一个解决方案,但我不知道如何将该解决方案转换为 C。文件中的每个值都是带符号的 short int 类型,并且有 1201x1201 个值。
const int SIZE = 1201;
signed short int matrix[SIZE][SIZE] = {0};
int main(int argc, const char * argv[])
{
using namespace std;
ifstream file("N49E013.hgt", ios::in|ios::binary);
unsigned char buffer[2];
for (int i = 0; i < SIZE; ++i)
{
for (int j = 0; j < SIZE; ++j)
{
if(!file.read( reinterpret_cast<char*>(buffer), sizeof(buffer) ))
{
cout << "Error reading file!" << endl;
system("PAUSE");
return -1;
}
matrix[i][j] = (buffer[0] << 8) | buffer[1];
}
}
【问题讨论】: