2010-03-05 95 views
0

我有一些MET數據我想驗證這會看起來像這些:在C++中驗證GPS字符串的最簡單方法是什麼?

char validBuffer[] = {"N51374114W1160437"}; 
char invalidBuffer[] = {"bad data\n"}; 
char emptyBuffer[] = {""}; 

我已經嘗試了簡單的sscanf,但失敗:

int _tmain(int argc, _TCHAR* argv[]) 
{ 

    char validBuffer[] = {"N51374114W1160437"}; 
    char invalidBuffer[] = {"bad data\n"}; 
    char emptyBuffer[] = {""}; 

    char ns = ' '; 
    char ew = ' '; 
    int northing = -999; 
    int easting = -999; 

    int paramsConverted = sscanf_s(validBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting); 
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", validBuffer, paramsConverted, ns, northing, ew, easting); 

    paramsConverted = sscanf_s(invalidBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting); 
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", invalidBuffer, paramsConverted, ns, northing, ew, easting); 

    paramsConverted = sscanf_s(validBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting); 
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", emptyBuffer, paramsConverted, ns, northing, ew, easting); 

    getchar(); 

    return 0; 
} 

給我:

Converted "N51374114W1160437"; Found 2 params [N,-999,",-1024] 
Converted "bad data 
"; Found 1 params [b,-999,",-1024] 
Converted ""; Found 2 params [N,-999,",-1024] 

我寧可不使用任何外部庫,如果可能的話,那麼有沒有這樣做,而不在同一時間解析它一個字符的一個不錯的簡單的方法?

+0

你這個標籤爲'C++'和'string',但使用的是'C'設施和'char'陣列。我猜測,通過標籤(和標題),C++解決方案是受歡迎的... – ezpz 2010-03-05 11:38:09

+1

PS。不要忘記驗證還包括座標範圍檢查。據推測他們在微觀上?所以'validBuffer'在法國的某個地方? – MSalters 2010-03-05 11:51:21

+0

@ezpz:是的,我不想限制答案非C++的建議.. – 2010-03-07 19:19:08

回答

3
+0

我所期待的東西不涉及增加額外的庫。 – 2010-03-08 10:14:37

+0

這是C++語言的一部分。 – 2010-03-08 10:37:51

+0

我應該多看一些搞我的嘴:-) – 2010-03-09 10:15:18

2
char validBuffer[] = {"N51374114W1160437"}; 
    char invalidBuffer[] = {"bad data\n"}; 
    char emptyBuffer[] = {""}; 

    if(strlen(validBuffer)!=18) 
    { 
//Error not valid buffer 
    } 
    char ns = validBuffer[0]; 
    char ew = validBuffer[9]; 
    int N = atoi(&validBuffer[1]); 
    int W = atoi(&validBuffer[10]); 
    if(N==0 || W==0) 
     //Error not valid buffer 

不簡單不是最好的,但有總比沒有好

+2

之前'atoi'返回'0'是模糊的。最好使用'strtol'或類似的。 – ezpz 2010-03-05 11:40:19

+0

如果你添加,作爲解決方案ezpz我會接受它作爲答案。 – 2010-03-08 10:14:13

2

您可以使用std::stringstream和流服務商...

#include <string> 
#include <sstream> 
#include <iostream> 

using namespace std; 

bool parse(string & s) { 
    stringstream ss(s); 
    char n = 0, w = 0; 
    int x = 0, y = 0; 
    ss >> n; 
    if (! ss.good()) return false; 
    ss >> x; 
    if (! ss.good()) return false; 
    ss >> w; 
    if (! ss.good()) return false; 
    ss >> y; 
    if (ss.bad()) return false; 

    cout << "Parsed {" << n << ", " << x << ", " 
     << w << ", " << y << "}" << endl; 
    return true; 
} 

int main() 
{ 

    string validBuffer = "N51374114W1160437"; 
    string invalidBuffer = "bad data\n"; 
    string emptyBuffer = ""; 

    if (! parse (validBuffer)) 
     cout << "Unable to parse: '" << validBuffer << "'" << endl; 
    if (! parse (invalidBuffer)) 
     cout << "Unable to parse: '" << invalidBuffer << "'" << endl; 
    if (! parse (emptyBuffer)) 
     cout << "Unable to parse: '" << emptyBuffer << "'" << endl; 

    return 0; 
} 

我使用上面的輸出:

Parsed {N, 51374114, W, 1160437} 
Unable to parse: 'bad data 
' 
Unable to parse: '' 
+0

實際上,你想要捕獲解析爲'GPS :: position :: operator <<(istream&input)'。剩餘的使用stringstream就變得微不足道了。 – MSalters 2010-03-05 11:48:08

+0

+1:這看起來像一個整潔的解決方案。謝謝! – 2010-03-08 10:16:13

1

考慮爲sscanf的有點特定格式參數():

int paramsConverted = sscanf(validBuffer, "%c%8d%c%7d", &ns, &northing, &ew, &easting);
+0

我嘗試過,但它似乎沒有任何區別? – 2010-03-08 10:15:04

相關問題