2014-02-18 75 views
0

我有以下數據:C++從數據讀取從文本文件

$GPVTG,,T,,M,0.00,N,0.0,K,A*13 

我需要讀取數據,但有空格的逗號之間,因此我不知道我應該怎麼讀數據。

另外,如何爲一組數據選擇GPVTG?例如:

GPVTG,,T,,M 
GPGGA,184945.00 
GPRMC,18494 
GPVTG,,T,,M,0 
GPGGA,184946.000,3409 

我已經嘗試使用:

/* read data line */ 
fgets(gpsString,100,gpsHandle); 
char type[10] = "GPVTG"; 
sscanf(gpsString," %GPVTG", &type); 
if (strcmp(gpsString, "GPTVG") == 0){ 
    printf("%s\n",gpsString); 
} 
+0

請說明。逗號之間有什麼「空白」?我沒有看到您提供的樣本數據中有任何空白。您是否指CSV記錄中的空白字段? – jia103

+1

你已經標記了C++,但發佈了C,你想要哪個答案? – dutt

+0

@ jia103我認爲他的意思是''''''''' – yizzlez

回答

0

如果你想使用基於fstream C++代碼風格:

fin.open(input); 

cout << "readed"; 

string str; 

getline(fin, str); // read one line per time 
0

這個怎麼樣

#include <istream> 
#include <sstream> 

class CSVInputStream { 
public: 
    CSVInputStream(std::istream& ist) : input(ist) {} 
    std::istream& input; 
}; 

CSVInputStream& operator>>(CSVInputStream& in, std::string& target) { 
    if (!in.input) return in; 
    std::getline(in.input, target , ','); 
    return in; 
} 

template <typename T> 
CSVInputStream& operator>>(CSVInputStream& in, T& target) { 
    if (!in.input) return in; 
    std::string line; 
    std::getline(in.input, line , ','); 
    std::stringstream translator; 
    translator << line; 
    translator >> target; 
    return in; 
} 

//-------------------------------------------------------------------- 
// Usage follow, perhaps in another file 
//-------------------------------------------------------------------- 

#include <fstream> 
#include <iostream> 

int main() { 
    std::ifstream file; 
    file.open("testcsv.csv"); 
    CSVInputStream input(file); 
    std::string sentence_type; 
    double track_made_good; 
    char code; 
    double unused; 
    double speed_kph; 
    char speed_unit_kph; 
    double speed_kmh; 
    char speed_unit_kmh; 
    input >> sentence_type >> track_made_good >> code; 
    input >> unused >> unused; 
    input >> speed_kph >> speed_unit_kph; 
    input >> speed_kmh >> speed_unit_kmh; 
    std::cout << sentence_type << " - " << track_made_good << " - "; 
    std::cout << speed_kmh << " " << speed_unit_kmh << " - "; 
    std::cout << speed_kph << " " << speed_unit_kph << std::endl;; 
} 

這分隔逗號與數值的讀取分離,並且可以重複使用 大多數其他逗號分隔的東西。

0

多數民衆贊成我會做

#include <iostream> 
#include <vector> 
#include <sstream> 
#include <fstream> 
#include <string> 

using namespace std; 

vector<string> &split(const string &s, char delim, vector<string> &elems) { 
    stringstream ss(s); 
    string item; 
    while (getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
    return elems; 
} 

vector<string> split(const string &s, char delim) { 
    vector<string> elems; 
    split(s, delim, elems); 
    return elems; 
} 


int main() 
{ 

    ifstream ifs("file.txt"); 

    string data_string; 

    while (getline(ifs, data_string)) 
    { 
      //i think you'd want to erase first $ charachter 
     if (!data_string.empty()) data_string.erase(data_string.begin()); 

      //now all data put into array: 
     vector<string> data_array = split (data_string, ','); 

     if (data_array[0] == "GPVTG") 
     { 
      //do whatever you want with that data entry 
      cout << data_string; 
     } 
    } 

    return 0; 
} 

應處理你的任務。所有空元素都將爲空「」字符串。詢問你是否需要其他東西。

分享功能的積分屬於Split a string in C++?答案。

+0

現在是完整答案 – user1849353