简体   繁体   中英

C++ reading from data from text file

I have the following data:

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

I need to read the data, however there are blanks in between the commas, therefore I am not sure how I should read the data.

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

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

I have tried using:

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

If you want use C++ style code based on fstream :

fin.open(input);

cout << "readed";

string str;

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

How about this

#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;;
}

This separates the comma separation from the reading of the values, and can be reused on most other comma separated stuff.

Thats what i'd do

#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;
}

Should handle your task. All empty elements will be empty "" strings in array. Ask if you need anything else.

Credits for split functions belong to Split a string in C++? answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM