string - C++ reading from data from text file -


I have the following data:

  $ GPVTG, T, M, 0.00, N I need to read the data, although there is a space between commas, so I'm not sure how to read the data. / P> 

In addition, how do I select GPVTG for a group of data only? For example:

  GPVTG, T, MGPGGA, 184945.00 GPRMC, 18494GPVTG, T, M, 0GPGGA, 184946.000,340 9  

I have tried to use it:

  / * read data line * / fgets (GPSstring, 100, GPS handle); Four types [10] = "GPVTG"; Sscanf (GPSstring, "% GPVTG", and type); If (SRCMP (GPS String, "GPTVVG") == 0} {printf ("% s \ n", GPSstring); }  

what should i do

  #include & Lt; Iostream & gt; #include & lt; Vector & gt; # Include & lt; Sstream & gt; # Include & lt; Fstream & gt; #include & lt; String & gt; using namespace std; Of vector & lt; String & gt; & Amp; Split (const string & amp; s, serial delimiter, vector & lt; string & amp; ames) {stringstream ss (s); String item; While (gateline (ss, items, delim)) {elems.push_back (item); } To return; } Vector & lt; String & gt; Split (Const String & S, Four Delim) {Vector & lt; String & gt; Elems; Division (S, Delim, Ames); Return eleven; } Int main () {ifstream ifs ("file.txt"); String data_string; While (getline (ifs, data_string)) // I think you want to erase the first $ charachter if (! Data_string.empty)) data_string.erase (data_string.begin ()); // Now insert all the data into the array: vector & lt; String & gt; Data_ar = division (data_string, ','); If (data_array [0] == "GPVTG") {// you want with that data entry kot & lt; & Lt; Data_string; }} Return 0; }  

All the empty elements array should handle empty string in your array. Ask if you need anything else.

The credit for the split works is from the North.


Comments