Code: |
#include <iostream> #include <fstream> #include <string> using namespace std; bool fileExists(const std::string& fileName) { std::fstream fin; fin.open(fileName.c_str(),std::ios::in); if( fin.is_open() ) { fin.close(); return true; } fin.close(); return false; } int main(){ string line; ifstream myfile ("COM3"); int i = 1; string filename = "C:\logs\gpslog"; filename.push_back((char)i+48); filename.append(".gpx"); while(fileExists(filename)){ i++; filename = "C:\logs\gpslog"; filename.push_back((char)i+48); filename.append(".gpx"); } ofstream outfile (filename.data()); if (myfile.is_open() && outfile.is_open()) { outfile << "<xml>" << endl; outfile << "<gpx>" << endl; outfile << "<trk>" << endl; outfile << "<trkseg>" << endl; do { getline (myfile,line); // Parsen der Daten und ausgeben } while (! myfile.eof() ); outfile << "</trkseg>" << endl; outfile << "</trk>" << endl; outfile << "</gpx>" << endl; myfile.close(); outfile.close(); } else cout << "Fehler!n"; return 0; } |
Code: |
#include <iostream> #include <fstream> #include <string> using namespace std; bool fileExists(const std::string& fileName) { std::fstream fin; fin.open(fileName.c_str(),std::ios::in); if( fin.is_open() ) { fin.close(); return true; } fin.close(); return false; } int main(){ string line; ifstream myfile ("COM3"); int i = 1; string filename = "C:logsgpslog"; filename.push_back((char)i+48); filename.append(".gpx"); while(fileExists(filename)){ i++; filename = "C:logsgpslog"; filename.push_back((char)i+48); filename.append(".gpx"); } ofstream outfile (filename.data()); // ifstream myfile ("/dev/ttyUSB0"); if (myfile.is_open() && outfile.is_open()) { outfile << "<xml>" << endl; outfile << "<gpx>" << endl; outfile << "<trk>" << endl; outfile << "<trkseg>" << endl; do { getline (myfile,line); try{ if(line.compare(1, 5, "GPRMC") == 0){ size_t pos1; size_t pos2; size_t pos3; size_t pos4; size_t pos5; size_t pos6; size_t pos7; size_t pos8; size_t pos9; size_t pos10; size_t pos11; string time_hour; string time_minute; string time_second; string status; string latG; string latM; string lonG; string lonM; string latsign; string lonsign; string speed; string course; string date_day; string date_month; string date_year; pos1 = line.find(",")+1; pos2 = line.find(",", pos1+1)+1; pos3 = line.find(",", pos2+1)+1; pos4 = line.find(",", pos3+1)+1; pos5 = line.find(",", pos4+1)+1; pos6 = line.find(",", pos5+1)+1; pos7 = line.find(",", pos6+1)+1; pos8 = line.find(",", pos7+1)+1; pos9 = line.find(",", pos8+1)+1; time_hour = line.substr(pos1, 2); time_minute = line.substr(pos1+2, 2); time_second = line.substr(pos1+4, 2); status = line.substr(pos2, 1); date_day = line.substr(pos9, 2); date_month = line.substr(pos9+2, 2); date_year = line.substr(pos9+4, 2); if(status.compare("A") == 0){ size_t poslat = line.find(".", pos3+1)+1; latG = line.substr(pos3, 2); latM = line.substr(pos3+2, pos4-pos3); latsign = line.substr(pos4, 1); size_t poslon = line.find(".", pos5+1)+1; lonG = line.substr(pos5, 3); lonM = line.substr(pos5+3, pos6-pos5); lonsign = line.substr(pos6, 1); speed = line.substr(pos7, pos8 - pos7 -1); course = line.substr(pos8, pos9 - pos8 -1); float x; float y; int xg; float xm; int yg; float ym; sscanf(latG.data(), "%d", &xg); sscanf(latM.data(), "%f", &xm); sscanf(lonG.data(), "%d", &yg); sscanf(lonM.data(), "%f", &ym); x = xg + (xm/60); y = yg + (ym/60); outfile.precision ( 9 ); outfile << "<trkpt lat="" << x << "" lon="" << y << "">" << endl; outfile << " <time>20" << date_year << "-" << date_month << "-" << date_day << "T"; outfile << time_hour << ":" << time_minute << ":" << time_second << "Z</time>" << endl; outfile << "<course>" << course << "</course>" << endl; outfile << "<speed>" << speed << "</speed>" << endl; outfile << "</trkpt>" << endl; } } } catch(std::exception& e){ } } while (! myfile.eof() ); outfile << "</trkseg>" << endl; outfile << "</trk>" << endl; outfile << "</gpx>" << endl; myfile.close(); outfile.close(); } else cout << "Unable to open filen"; return 0; } |