我有一些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]
我寧可不使用任何外部庫,如果可能的話,那麼有沒有這樣做,而不在同一時間解析它一個字符的一個不錯的簡單的方法?
你這個標籤爲'C++'和'string',但使用的是'C'設施和'char'陣列。我猜測,通過標籤(和標題),C++解決方案是受歡迎的... – ezpz 2010-03-05 11:38:09
PS。不要忘記驗證還包括座標範圍檢查。據推測他們在微觀上?所以'validBuffer'在法國的某個地方? – MSalters 2010-03-05 11:51:21
@ezpz:是的,我不想限制答案非C++的建議.. – 2010-03-07 19:19:08