2017-08-13 72 views
-3

我需要從Swift中的二進制.hgt文件讀取高程數據。我找到了this result for c,但我無法將它遷移到Swift。在Swift中讀取二進制(.hgt)文件(將代碼從C++遷移到swift)

#include <stdio.h> 

#define SIZE 1201 
signed short int matrix[SIZE][SIZE] = {0}; 

int main(int argc, const char * argv[]) 
{ 
FILE *fp = fopen("N49E013.hgt", "rb");  

unsigned char buffer[2]; 
for (int i = 0; i < SIZE; ++i) 
{ 
    for (int j = 0; j < SIZE; ++j) 
    { 
     if (fread(buffer, sizeof(buffer), 1, fp) != 1) 
     { 
      printf("Error reading file!\n"); 
      system("PAUSE"); 
      return -1; 
     } 
     matrix[i][j] = (buffer[0] << 8) | buffer[1];  
    } 
} 

fclose(fp); 
} 

回答

2

#define SIZE 1201

這定義了一個名爲 'SIZE' 不變,所以這樣做:

let size = 1201

下一個:

FILE *fp = fopen("N49E013.hgt", "rb");

這將打開一個文件讀。我們能做到這一點。在'延遲'區塊中關閉文件,這樣無論如何,文件在完成後都會關閉。

// change the path below to the correct path 
let handle = try FileHandle(forReadingFrom: URL(fileURLWithPath: "/path/to/N49E013.hgt")) 
defer { handle.closeFile() } 

現在來構造矩陣。我們要創建size數組,其中每個數組都有size個元素,從文件中讀取。原來用兩個嵌套的for循環,而斯威夫特支持函數編程結構,我們可以用它來多一點優雅的做到這一點:

let matrix = try (0..<size).map { _ in 
    try (0..<size).map { _ -> Int in 
     // Unfortunately, FileHandle doesn't have any decent error-reporting mechanism 
     // other than Objective-C exceptions. 
     // If you need to catch errors, you can use fread as in the original, 
     // or use an Objective-C wrapper to catch the exceptions. 

     let data = handle.readData(ofLength: 2) 

     if data.count < 2 { throw CocoaError(.fileReadCorruptFile) } 

     return (Int(data[0]) << 8) | Int(data[1]) 
    } 
} 

想那應該做到這一點。