Hi all.

I need to read a .txt document with my c++ program. My .txt is data that I take from a GPS and it has different kind of data

i.e. <timeStamp> <status> <latitude> <longitude> <altitude> <numSatellites> <HDOP> <X> <Y> <Z> <X2D> <Y2D> <Xdiff3D-2D> <Ydiff3D-2D> <NMEA_GGAstring><CR>

and the information is like this:

1213891110.84 0 41.3831338 2.1161887 66.2 7 1.2 -294.85 -554.07 -56.49 -336.30 -559.84 41.46 5.77 $GPGGA,155830.00,4122.98803,N,00206.97132,E,1,07,1.2,66.21,M,51.31,M,0.0,,*50

1213891111.96 0 41.3831343 2.1161892 66.2 7 1.2 -294.78 -554.07 -56.49 -336.23 -559.83 41.45 5.77 $GPGGA,155831.00,4122.98806,N,00206.97135,E,1,07,1.2,66.17,M,51.31,M,0.0,,*56

So the problem is to create the code to open and read this.

My code is....

#include "gpsfromFile.h"

/**
 Default constructor. Reads configuration file, opens data and log files and opens and onfigures the serial port the config file says
*/
CgpsfromFile::CgpsfromFile(int partnerID, int robotID, char *labelString) : Cgps(partnerID,robotID,labelString)
{
	
	cout << "Here is where I need to implement constructor. Open file define in labelstring" << endl;

}

/**
 Default destructor. Closes serial port
*/
CgpsfromFile::~CgpsfromFile()
{
  	cout << "Here is where I implement destructor. Close file define in labelstring" << endl;
}


*/
void CgpsfromFile::process()
{
	cout << "Here is where I need to implement the process to read line by line the filename with fscanf or fread and change the data in the define variables in the protected of gps.h" << endl;
}

-------------------------------------------------------------------------------------------

This is what I got in gpsfromFile.h

#ifndef gpsfromFile_h
#define gpsfromFile_h


#include <sys/time.h>
#include <termios.h>
#include <fcntl.h>

#include "../gps/gps.h"

using namespace std;

/**
  CgpsfromFile implements a class for a gps as read from a file based on the generic class for gps, Cgps
*/
class CgpsfromFile : public Cgps
{
  protected:

	struct stime
	{
		unsigned int hour;
		unsigned int minute;
		unsigned int second;
		unsigned int cen_second;
	};

  public:

    	CgpsfromFile(int partnerID, int robotID, char *labelString); /**<default constructor, opens file to read from */
    	virtual ~CgpsfromFile(); /**<default destructor, closes file, data and log files*/
    	void process();

//	void printData(); /**<Prints data to gpsDataFile*/
};
#endif

-----------------------------------------------------------------------------------------------------

and this is gps.h


#ifndef gps_h
#define gps_h

//include
#include <math.h>
#include <yarp/os/Network.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Bottle.h>
#include "../logDataProcess/logDataProcess.h"

//const double deg2rad= 3.141592653589793/180.0;

using namespace yarp::os;

/**
  Cgps implements a generic class for gps devices, based on the class ClogDataFiles which manages with Data and Log files
*/
class Cgps : public ClogDataProcess
{
	protected:
                double latitude, longitude, altitude; /**<Earth coordinates (degrees, degrees, meters) */
                int numSatellites; /**<Number of satellites */
                float HDOP; /**<Horizontal Dilution of Precision */
                double xx2d, yy2d, xx, yy, zz; /**<Coordinates wrt map origin (meters) */
                char NMEAGGA[88]; /**<NMEA chain buffer type GGA. Max lenght is 82 + 1 for \\n*/
		char stringData[250];/**<auxiliar string to write data to be printed in the data file*/

		BufferedPort<Bottle> output; /**<YARP port to send the device data*/

	public: 
		Cgps(int ptid, int rid, char *labelString); /**<constructor*/
		virtual ~Cgps(); /**<destructor*/
		virtual void process()=0; /**<loads gps data to the variables. Returns an error code. 0 is OK! */
		void printData(); /**<Prints data to gpsDataFile*/
		void sendData(); /**<writes device data to the yarp output port*/
};

#endif

Recommended Answers

All 10 Replies

This is something like how to code the program to read each line of the file.

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
...
...
ifstream in("filename.txt");
std::string input;
float timestamp,latitude, longitude, altitude;
while( getline(in, input) )
{
    // split it into individual parts
    stringstream str(line);
    str >> timestamp >> latitude >> longitude >> altitude; // etc. etc for each variable
    // create class and initialize with above data
    <snip>
}

thanks for being so quick with your help. I will try it and let you know....

Thanks...

still having problem with my code, can you be more specific about the code.

Can you explain to me step by step how to write the code to achieve my goal?

Thanks

Jason

If you have a file, and you have pieces of data in the file seperated by whitespace, and you know the type and order of those pieces of data, you can just use an ifstream object. For example, if you have a file that has a string with someone's first name, their age, and their secret floating point number, do this:

std::string name;
int age;
float secretNum;
ifstream fin("file.txt");//create an input file stream that reads from file.txt
fin >> name >> age >> secrectNum;//read in the name, age, and secret number into the variables name, age, and secretNum.

If you have actual code that won't compile (or doesn't do what you think it should), post it here.

Here is a little more specific info. I have no idea how to convert that double timestamp in the file to the individual time parts that's in the Cgps class. How was that double number created ? You will probably have to get decode information from whatever program created it.

// read the file
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
...
...
...
// an array of Cgps classes
vector<Cgps> list; 

ifstream in("filename.txt");
std::string input;
float timestamp,latitude, longitude, altitude;
while( getline(in, input) )
{
    // split it into individual parts
    stringstream str(line);
    str >> timestamp >> latitude >> longitude >> altitude; // etc. etc for each variable
    // create class and initialize with above data
   Cgps gps;
   gps.latitude = latitude;
   // etc etc for each field that was read.
   ...
   // now add to the array
   list.push_back(gps);    
}
1213891110.84 0 41.3831338 2.1161887 66.2 7 1.2 -294.85 -554.07 -56.49 -336.30 -559.84 41.46 5.77 $GPGGA,155830.00,4122.98803,N,00206.97132,E,1,07,1.2,66.21,M,51.31,M,0.0,,*50

That file format seems to be inconsistent -- are the fields separated by spaces or by commas? Or is that last part beginning with $ all one field?

Ancient Dragon.

I wasn't the one that create the .txt program but I will ask to my mentor and I will get back to you with an answer.

And thank you for all your advise.

Jason

Ancient Dragon.

we can ignore the part that start with the $ because all the important information is before that, so the answer would be that the data is separate by a blanck space.

and about the code the data should be read it a save the data that's been read in a document or save it in a list all the data that is been read it. Not to put an object in a list.

THank you for your time Ancient Dragon.


Jason

we can ignore the part that start with the $ because all the important information is before that, so the answer would be that the data is separate by a blanck space.

The the code I posted will probably work because getline() will read the entire line, then use stringstream to separate it into individual parts, tossing out the part that starts with the $ symbol. You could do it as CoolGamer suggest too, but it may not be as immediately apprarent what is going on when you read the code in a month or two.

and about the code the data should be read it a save the data that's been read in a document or save it in a list all the data that is been read it. Not to put an object in a list.

Jason

I don't understand what you are trying to tell us. After reading each line either save the data into another file (why???) or add it to a list -- which is what I did in my previous post. In order to put the data into an array you have to add the data to a class or structure, then put an instance of that into the array. It doesn't matter whether its a Cgps class or something else you design. I used Cgps because that's the code you posted. That's the most reasonable way to keep all that data organized within your program.

Hi.

I write a code with all your help and now what I need to do is to put the value that I'm reading in a variable that is already created in a program.

I #include the program but how put the value to that variable i.e latitude (is in one code before mine) in my code I have variable1 and I need that variable1 value goes to latitude....and therefore variable2 to altitude...variable3....variable14...to others like numSatellites...etc


the code looks like this....
---------------------------------------------------------

#include "gpsfromFile.h"
#include <iostream.h>
#include <fstream.h>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main () {
char line[256];
float variable1, variable3, variable4, variable5, variable7, variable8, variable9, variable10, variable11, variable12, variable13, variable14;
int variable2, variable6;

{
/**
 Default constructor. Reads configuration file, opens data and log files and opens and onfigures the serial port the config file says
*/
CgpsfromFile::CgpsfromFile(int partnerID, int robotID, char *labelString) : Cgps(partnerID,robotID,labelString)
{
	ifstream myfile ("labelString");
	

/**
 Default destructor. Closes serial port
*/
CgpsfromFile::~CgpsfromFile()
{
myfile.close();

}

/**
  read reply from AsteRx1. Virtual function in base class CGPS
*/
void CgpsfromFile::process()
{
myfile.getline(line,256);
while (myfile.good()) {
myfile.getline(line,256);
sscanf(line,"%f %d %f %f %f %d %f %f %f %f %f %f %f %f",&variable1, &variable2, &variable3, &variable4, &variable5, &variable6, &variable7, &variable8,&variable9,&variable10,&variable11,&variable12,&variable13,&variable14);
cout << variable1 << " " << variable2 << " " << variable3 << " " << variable4 << " " << variable5 << " " << variable6 << " " << variable7 << " " << variable8 << " " << variable9 << " " << variable10 << " " << variable11 << " " << variable12 << " " << variable13 << " " << variable14 << " " << endl;

}
}
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.