Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ build an array based on data from file matching criteria

Status
Not open for further replies.

sholva

IS-IT--Management
Jul 21, 2006
1
US
I am trying to get data from a tab delimited file(example of a few
lines below). I want to count the number of authentications per hour.
The first and fourth line is an example of a line I would want to
count, the middle two are examples of other lines in the file that I
want to ignore. The approach I am looking at is reading the first
section and using the hour field as the address in the array.

int count = 0;
If (string contains "Successfully")
AuthCount[HourValue] = count ++

So the data below would result in
AuthCount[14]=1
AuthCount[15]=1

Any suggestions on how to write this?

2006-07-20 14:31:52 User.Info ServerIP Perfigo: Authentication:[User
MAC ## User IP] username - Successfully logged in, Provider:
authservername, L2 MAC address: User MAC

2006-07-20 14:31:52 User.Info ServerIP Perfigo: Administration:User MAC
added to certified device list

2006-07-20 15:12:49 User.Info ServerIP Perfigo: Miscellaneous:Overwrote
1 logs in the past 10 minutes to keep the event log limit.

2006-07-20 15:31:52 User.Info ServerIP Perfigo: Authentication:[User
MAC ## User IP] username - Successfully logged in, Provider:
authservername, L2 MAC address: User MAC
 
I was feeling generous today. :)
The example you posted had spaces instead of tabs, so I took a guess where the tabs should be.
Code:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <functional>
#include <algorithm>

using namespace std;

typedef vector<string>	StrVec_t;


class NotContains : public binary_function<StrVec_t, const string, bool>
{
public:
	bool operator()( StrVec_t&  it, const string&  str ) const
	{
		if ( it.size() >= 5 )
		{
			if ( it[ 4 ].find( str ) != string::npos )
			{
				return false;
			}
		}

		return true;
	}
};

class HourEquals : public binary_function<StrVec_t, int, bool>
{
public:
	bool operator()( StrVec_t&  it, int  hour ) const
	{
		if ( it.size() >= 2 )
		{
			// Extract the hour from the hh:mm:ss string.
			size_t pos = it[ 1 ].find( ":" );
			string strHour( it[ 1 ].substr( 0, pos ) );

			// Convert hh string into an int.
			stringstream ss;
			ss << strHour;
			int hour2 = 0;
			ss >> hour2;

			// Now compare the two hours.
			if ( hour == hour2 )
			{
				return true;
			}
		}

		return false;
	}
};


int main( int argc, char* argv[] )
{
	if ( argc != 2 )
	{
		cout << "No input file was specified!" << endl;
		return 1;
	}

	ifstream inFile( argv[ 1 ] );

	if ( !inFile )
	{
		cout << "Error opening file!" << endl;
		return 1;
	}

	string				line;
	vector<StrVec_t>	records;

	// Read & parse file into the records vector.
	while ( getline( inFile, line ) )
	{
		StrVec_t fields;

		while ( line.length() > 0 )
		{
			size_t pos = line.find( "\t" );		// Find next tab.

			// If the line starts with a tab, delete it and repeat loop.
			if ( pos == 0 )
			{
				line.erase( 0, 1 );
				continue;
			}

			if ( pos != string::npos )
			{
				// Put each field in the vector.
				string field = line.substr( 0, pos );
				line.erase( 0, pos + 1 );
				fields.push_back( field );
			}
			else
			{
				fields.push_back( line );
				line.erase();
			}
		}

		records.push_back( fields );
	}

	// Remove all non-authentication records.
	vector<StrVec_t>::iterator start	= records.begin();
	vector<StrVec_t>::iterator end		= records.end();
	string auth( "Authentication:" );

	records.erase( remove_if( start, end, bind2nd( NotContains(), auth ) ), records.end() );
	end = records.end();

	for ( int hour = 0; hour < 24; ++hour )
	{
		size_t n = count_if( start, end, bind2nd( HourEquals(), hour ) );

		cout << "At hour " << hour << " there were " << n << " Authentications." << endl;
	}

	return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top