View Single Post
Old 03-05-2005, 01:08 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include <iostream>
#include <fstream>
#include <ios>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

void print_vector_hex(vector<unsigned>& );
void fetch_block(ifstream&, double& ,vector<unsigned>& );
void fetch_and_print_all_blocks(ifstream& , double&, vector<unsigned>&);

int main(int argc, char *argv[])
{
  vector<unsigned> ip_signature;
  double arr_time(0);
  ifstream ifs("in.txt");  
  fetch_and_print_all_blocks(ifs, arr_time, ip_signature);
  cin.get();
  return 0;
}

//fetch_block() assumes a block is:
// a) Starts with a double.
// b) After a double, it continues with unknown numbers of unsinged ints.
//End if newline or EOF is found.
void fetch_block(ifstream& ifs, double& time, vector<unsigned>& vec)
{
  string sPeeked;
  unsigned val;
  istringstream istr;
  
  //Fetch double from block if file is not empty.
  if( getline(ifs, sPeeked) )
  {
    istr.str(sPeeked);
  }  
  istr>>time;
  
  //Now fetch the rest of the block if any unsigned is present.
  while( getline(ifs, sPeeked) && sPeeked != "")
  {
    istr.clear();
    istr.str(sPeeked);
    istr>>hex>>val;
    vec.push_back(val);  
  }   
}  

void print_vector_hex(vector<unsigned>& vec)
{
  for(unsigned j = 0; j < vec.size(); ++j)
  {
    cout<<hex<<vec[j]<<endl;
  }  
}

void fetch_and_print_all_blocks(ifstream& ifs, double& arr_time, vector<unsigned>& ip_signature)
{
  while( ifs.peek() != EOF )
  {
    fetch_block(ifs, arr_time, ip_signature);
    //Let's print the arr_time and a the block.
    cout<<"arr_time = "<<arr_time<<endl;
    print_vector_hex(ip_signature);
    cout<<endl;
    //Empty the vector for the (possible) new block or data.
    ip_signature.erase(ip_signature.begin(), ip_signature.end());
  }
}
__________________
Valmont is offline   Reply With Quote