GPS_Logging & Evaluation

Jun 1, 2016 at 8:28am
closed account (D3R4216C)
Hello, I have the following Problem:

I want to calculate the standard derivation between two GPS Modules, so I can get an estimate on how accurate the one module is. I have logged it into a normal text file and it is now filled with lots of unnecessary Data. The calculating Part is no Problem but I please need help with the Code two filter out the parts i need and the parts i don't need, for example:

From this message:

GPRMC 092055.00,A,4850.54638,N,01043.20367,E,7.405

i only need:

4850.54638 N 01043.20367 E

I know there was a possibility to do this in C with strtok but I was wondering how I can program this ? I would really appreciate help.
Jun 1, 2016 at 9:09am
Read your file line by line. To get the data from a line you can use a stringstream and getline.
A line has 7 tokens seperated by ','
Skip the first two, read the 4 you need and skip the rest.
Jun 1, 2016 at 9:06pm
closed account (D3R4216C)
Thanks for the quick help, can you help me a little more with the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
    
string filename, line;
    int line_counter {0};
    
    cout << "Name of file:" << endl;
    cin >> filename;
    
    fstream pfile;
    pfile.open(filename, ios::in | ios::out | ios::app);
    
    while (getline(pfile, line)) {
        line_counter++;
    }
    
    return 0;
}
Jun 2, 2016 at 8:24am
Here is an example to extract the data from a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct GPS
{
  double d1;
  char c1;
  double d2;
  char c2;
};

GPS ParseData (const string& input)
{
  GPS retval;
  string buffer;
  char sep;

  istringstream iss (input);
  getline (iss, buffer, ','); // skip first entry
  getline (iss, buffer, ','); // skip second entry
  iss >> retval.d1; // read first number
  iss >> sep; // skip next ','
  iss>> retval.c1; // read first char
  iss >> sep; // skip next ','
  iss >> retval.d2; // read next number
  iss >> sep; // skip next ','
  iss >> retval.c2; // read last char of interest

  return retval;
}

void ShowGPS (const GPS& gps)
{
  cout << "GPS data: \n";
  cout << "d1 = " << gps.d1 << "\tc1 = " << gps.c1 << "\td2 = " << gps.d2 << "\tc2 = " << gps.c2 << "\n";
}

int main () 
{
  string s ("GPRMC 092055.00, A, 4850.54638, N, 01043.20367, E, 7.405");
  GPS gps = ParseData (s);
  ShowGPS (gps);

  system ("pause");
  return 0;
}

Maybe you can integrate it in your program. Since I have no idea what this data mean I couldn't find any meaningful names for the GPS struct.
Last edited on Jun 2, 2016 at 8:24am
Jun 2, 2016 at 3:43pm
closed account (D3R4216C)
Thanks a lot, will try it first thing tomorrow!
Jun 3, 2016 at 3:15pm
closed account (D3R4216C)
Ok this is was a great help already, last problem I need to read the lines from a source.txt File and write them into another new.txt, thats really my last request!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct GPS
{
    double d1;
    char c1;
    double d2;
    char c2;
};

GPS ParseData (const string& input)
{
    GPS retval;
    string buffer;
    char sep;
    
    istringstream iss (input);
    getline (iss, buffer, ','); // skip first entry
    getline (iss, buffer, ','); // skip second entry
    iss >> retval.d1; // read first number
    iss >> sep; // skip next ','
    iss>> retval.c1; // read first char
    iss >> sep; // skip next ','
    iss >> retval.d2; // read next number
    iss >> sep; // skip next ','
    iss >> retval.c2; // read last char of interest
    return retval;
}

void ShowGPS (const GPS& gps)

{
    cout << "GPS data: \n";
    cout << "d1 = " << gps.d1 << "\tc1 = " << gps.c1 << "\td2 = " << gps.d2 << "\tc2 = " << gps.c2 << "\n";
}

int main ()

{
    string sfilename;
    
    cout << "\nEnter Name of File:" << endl;
    cin >> sfilename;
    
    fstream sourcefile;
    sourcefile.open(sfilename, ios::in | ios::out | ios::app);
    
    if (!sourcefile) {cerr << "Error opening File" << endl;}
   
    
    sourcefile.close();
    
    string s ("GPRMC 092055.00, A, 4850.54638, N, 01043.20367, E, 7.405");
    GPS gps = ParseData (s);
    ShowGPS (gps);
    return 0;
}
Jun 3, 2016 at 4:44pm
Where are you even reading anything from your file?

Why did you open the stream for both read and write? I thought you wanted one file for reading and another file for writing. If this is the case I suggest you use an ifstream for the input stream, and an ofstream for the output stream. It also looks like you need to review some documentation for basic file input and output:

http://www.cplusplus.com/doc/tutorial/files/


Topic archived. No new replies allowed.