Click here to Skip to main content
15,918,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am reading a text file where my inputs are as follows

date Fri Sep 1 02:11:40.195 pm 2017
base hex  timestamps absolute
internal events logged
// version 9.0.0
Begin Triggerblock Fri Sep 1 02:11:40.195 pm 2017
   0.000000 Start of measurement
   0.002893 1  201             Rx   d 8 06 0D 00 B0 89 00 0D E7  Length = 227925 BitCount = 118 ID = 513
   0.003133 1  280             Rx   d 8 1B 0C 7C F1 E8 75 39 67  Length = 221910 BitCount = 115 ID = 640
   0.003375 1  281             Rx   d 8 6B 0C 00 4E F4 07 8F 18  Length = 223925 BitCount = 116 ID = 641
   0.003623 1  282             Rx   d 8 DA 0C 23 FA 01 02 2C 04  Length = 227910 BitCount = 118 ID = 642


i need to make the file read from line 6 and store each of them into class variable and also should skip reading length,bitcount and ID

can anyone suggest me how i could set them

my class is as follows
C++
class data_Read
{
public:
	double time;
	unsigned int state;
	std::string  ID;
	std::string status;
	std::string type;
	unsigned int byte_lent = 8;
        std::string message[];

public:
	void readData(std::istream& is, data_Read& data);
};

and readdata function like this

void data_Read::readData(std::istream& is, data_Read& data)
{
	std::string line;

	for (int i = 0; i <= 5; i++)
	{
		std::getline(is, line);
	}
	
	while (std::getline(is, line))
	{
		std::istringstream iss(line);

		iss >> data.time;
		iss >> data.state;
		iss >> data.ID;
		iss >> data.status;
		iss >> data.type;
		
		for (int i = 1; i <= data.byte_lent; i++)
		{
			iss >> data.message[i];
		}
	}

	cout << data.time << "/t" << data.state << "/t" << data.ID << "/t" << data.status << "/t" << data.type << "/t" << data.message;
}


what changes i could do in order to get the output as i want

What I have tried:

i tried initialising line but did not work and giving loop to read first 5 lines and later next
Posted
Updated 8-Dec-17 3:51am
v2

What do you get and what do you expect?

Also, why have you changed your message member to a std::string array instead of letting it an uint8_t array as used in your previous question Read and store array of hexadecimal data[^]?

The only problem I can see is that you forgot to read the message length byte_lent:
iss >> data.time;
iss >> data.state;
iss >> data.ID;
iss >> data.status;
iss >> data.type;
// Reading the length is missing in your code
iss >> data.byte_lent;
for (unsigned int i = 0; i < data.byte_lent; i++)
{
    // Read msg data bytes here
}
[EDIT: Clarifying]
Quote from (not a) solution:
and ignore 8 and length, bitcount and ID
If you want to ignore the length, you still have to read it from the stream. Otherwise, it will be read as first message data.

The remaining data after the message bytes are skipped by just not reading further from the stream buffer but processing the next line.
[/EDIT]
 
Share this answer
 
v4
The following program
C++
#include <iostream>
#include <sstream>
#include <string>
#include <array>
#include <vector>
#include <iomanip>

using namespace std;

struct Data
{
  static constexpr size_t SIZE = 8;
  double time;
  int id;
  int status;
  string type;
  string dir;
  std::array<uint8_t, SIZE> d; 
  bool parse_line(const string & line);
};

ostream & operator << ( ostream & os, const Data & data);

int main()
{
  string line;
  vector <Data> v;

  while ( getline(cin, line) )
  {
    Data d;
    if ( d.parse_line(line) )
      v.push_back( d );
  }
  
  for (const auto & d : v)
    cout << d << endl;
}

ostream & operator << ( ostream & os, const Data & data)
{
  os << "time " << data.time << "\tid " << data.id << "\tstatus " << data.status << "\ttype " << data.type << "\tdir " << data.dir << "\tdata ";
  for (size_t n=0; n < Data::SIZE; ++n)
    os << setw(4) << static_cast<unsigned int>(data.d[n]);
  return os;
}

bool Data::parse_line( const string & line )
{
  istringstream iss(line);

  iss >> time;
  if ( ! iss ) return false;
  iss >> id;
  if ( ! iss ) return false;
  iss >> status;
  if ( ! iss ) return false;
  iss >> type;
  if ( ! iss ) return false;
  iss >> dir;
  if ( ! iss ) return false;

  int count;
  iss >> count;
  if ( ! iss ) return false;
  for ( size_t n= 0; n< SIZE; ++n)
  {
    size_t pos;
    string hex;
    iss >> hex;
    if (!iss) return false;
    d[n] = static_cast<uint8_t>( stoul(hex, &pos, 16));
    if ( pos != 2 ) return false;
  }
  return true;
}

fed with your input data (redirect it to get standrd iputfrom your file) produces the following output:
time 0.002893	id 1	status 201	type Rx	dir d	data    6  13   0 176 137   0  13 231
time 0.003133	id 1	status 280	type Rx	dir d	data   27  12 124 241 232 117  57 103
time 0.003375	id 1	status 281	type Rx	dir d	data  107  12   0  78 244   7 143  24
time 0.003623	id 1	status 282	type Rx	dir d	data  218  12  35 250   1   2  44   4
 
Share this answer
 
Comments
Member 13475664 8-Dec-17 10:01am    
thanks a lot for the help
CPallini 8-Dec-17 10:06am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900