
Kat H.
asked 04/26/21Web Log Parser. Submit to elearn: Make sure to name files EXACTLY as specified - including capitalization. Do not submit extra files. Do not zip or otherwise compress files unless told to.
Objective
Youwill be able to read data from a file and use string functions to process text.
Assignment Instructions
Should be able to compile and run your program with:
Your task is to write a program that parses the log of visits to a website to extract some information about the visitors. Your program should read from a file called WebLog.txt which will consist of an unknown number of lines. Each line consists of the following pieces of data separated by tabs:
Where Date is in the format d-Mon-yy (day, Month as three letters, then year as two digits) and time is listed in 24-hour time.
Read in the entire file and print out each record from April (do not print records from other months) in the format:
Where mm/dd/yy is a date in the format month number, day number, year and the time is listed in 12-hour time (with AM/PM).
For example, the record:
Should be printed as something like:
Make sure that you read the right input file name. Capitalization counts!
Do not use a hard-coded path to a particular directory, like "C:\Stuff\WebLog.txt"
. Your code must open a file that is just called "WebLog.txt"
.
Do not submit the test file; I will use my own.
Here is a sample data file you can use during development. Note that this file has 100 lines, but when I test your program, I will not use this exact file. You cannot count on there always being exactly 100 records.
Hints
- Start small! Making sure your program works correctly on 1 line, or three lines is a lot easier than 100.
- Remember, my test file will have a different number of lines.
-
You can read in something like
13:26:16
all as a string, or as an int, a char (:), an int, a char (:), and another int. - If you need to turn a string into an int or a double, you can use this method:
1 Expert Answer

Patrick B. answered 04/28/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _TDate
{
int month;
int day;
int year;
} * TDate;
typedef struct _TTime
{
int hour;
int min;
int sec;
} * TTime;
typedef struct _TWebDataRec
{
char ipAddress[15];
char userName[55];
struct _TDate logonDate;
struct _TTime logonTime;
struct _TTime duration;
} * TWebDataRec;
MonthStr2Num ( string monthNameStr)
{
int iMonthNumReturn = 0;
if (monthNameStr.compare("Jan")==0)
{
iMonthNumReturn=1;
}
else if (monthNameStr.compare("Feb")==0)
{
iMonthNumReturn=2;
}
else if (monthNameStr.compare("Mar")==0)
{
iMonthNumReturn=3;
}
else if (monthNameStr.compare("Apr")==0)
{
iMonthNumReturn=4;
}
else if (monthNameStr.compare("May")==0)
{
iMonthNumReturn=5;
}
else if (monthNameStr.compare("Jun")==0)
{
iMonthNumReturn=6;
}
else if (monthNameStr.compare("Jul")==0)
{
iMonthNumReturn=7;
}
else if (monthNameStr.compare("Aug")==0)
{
iMonthNumReturn=8;
}
else if (monthNameStr.compare("Sep")==0)
{
iMonthNumReturn=9;
}
else if (monthNameStr.compare("Oct")==0)
{
iMonthNumReturn=10;
}
else if (monthNameStr.compare("Nov")==0)
{
iMonthNumReturn=11;
}
else if (monthNameStr.compare("Dec")==0)
{
iMonthNumReturn=12;
}
else
{
//should not happen
}
return(iMonthNumReturn);
}
void WebDataRecDump (TWebDataRec webDataRec, char * strMsg)
{
if (strMsg!=NULL)
{
cout << "******************************************" << endl;
cout << strMsg << endl;
}
cout << "**********************************************" << endl;
cout << " ip addr >" << webDataRec->ipAddress << "<" << endl;
cout << " username " << webDataRec->userName << "<" << endl;
cout << " logon date/time " << webDataRec->logonDate.month<< " " << webDataRec->logonDate.day << " " << webDataRec->logonDate.year
<< webDataRec->logonTime.hour << " " << webDataRec->logonTime.min << " " << webDataRec->logonTime.sec << endl;
cout << "duration time " << webDataRec->duration.hour << " " << webDataRec->duration.min << " " << webDataRec->duration.sec << endl;
}
void Parse(string strInbuff, TWebDataRec webDataRec)
{
char dateTimeStamp[55];
char durationTime[55];
char inbuff[255];
char logonDateBuff[55];
char logonTimeBuff[55];
char durationTimeBuff[55];
char numBuff[25];
strcpy(inbuff,strInbuff.c_str());
//cout << ">" << inbuff << "<" << endl;
//ip address
strcpy(webDataRec->ipAddress,strtok(inbuff," "));
//cout << " ip adrress >" << webDataRec->ipAddress << "<" << endl;
//username
strcpy(webDataRec->userName,strtok(NULL," "));
// cout << " username >" << webDataRec->userName << "<" << endl;
//logon date
strcpy(logonDateBuff,strtok(NULL," "));
//cout << " logon date >" << logonDateBuff << "<" << endl;
//logon time
strcpy(logonTimeBuff,strtok(NULL," "));
//cout << " logon time >" << logonTimeBuff << "<" << endl;
//duration
strcpy(durationTimeBuff,strtok(NULL," "));
//cout << " duration time >" << durationTimeBuff << "<" << endl;
//parses logon date
strcpy(numBuff,strtok(logonDateBuff,"-"));
webDataRec->logonDate.day= atoi(numBuff);
//cout << "logon day " << webDataRec->logonDate.day << endl;
strcpy(numBuff,strtok(NULL,"-"));
string name(numBuff);
webDataRec->logonDate.month = MonthStr2Num(numBuff);
//cout << "logon month " << webDataRec->logonDate.month << endl;
strcpy(numBuff,strtok(NULL,"-"));
webDataRec->logonDate.year = atoi(numBuff);
//cout << "logon year " << webDataRec->logonDate.year << endl;
//parses logon time
strcpy(numBuff,strtok(logonTimeBuff,":"));
webDataRec->logonTime.hour = atoi(numBuff);
strcpy(numBuff,strtok(NULL,":"));
webDataRec->logonTime.min = atoi(numBuff);
strcpy(numBuff,strtok(NULL,":"));
webDataRec->logonTime.sec = atoi(numBuff);
//cout << " logon time componenets hour:min:sec >" << webDataRec->logonTime.hour << " : " << webDataRec->logonTime.min << " : " << webDataRec->logonTime.sec << endl;
//duration time
if (strlen(durationTimeBuff)>5)
{
strcpy(numBuff,strtok(durationTimeBuff,":"));
webDataRec->duration.hour = atoi(numBuff);
strcpy(numBuff,strtok(NULL,":"));
webDataRec->duration.min = atoi(numBuff);
strcpy(numBuff,strtok(NULL,":"));
webDataRec->duration.sec = atoi(numBuff);
}
else
{
webDataRec->duration.hour=0;
strcpy(numBuff,strtok(durationTimeBuff,":"));
webDataRec->duration.min = atoi(numBuff);
strcpy(numBuff,strtok(NULL,":"));
webDataRec->duration.sec = atoi(numBuff);
}
//cout << " logon time componenets hour:min:sec >" << webDataRec->duration.hour << " : " << webDataRec->duration.min << " : " << webDataRec->duration.sec << endl;
}
Go()
{
ifstream infile((char*)"E:\\weblog.txt", ifstream::in);
char inbuff[255];
struct _TWebDataRec webDataRec;
char outbuff[255];
while (!infile.eof())
{
infile.getline(inbuff,255,'\n');
Parse(inbuff,&webDataRec);
if (webDataRec.logonDate.month!=4) { continue; }
memset(outbuff,0,255);
char AmPmStr[3];
int iHour;
if (webDataRec.logonTime.hour>12)
{
iHour = webDataRec.logonTime.hour-12;
strcpy(AmPmStr,"PM");
}
else
{
iHour = webDataRec.logonTime.hour;
strcpy(AmPmStr,"AM");
}
sprintf(outbuff,"%s %d/%d/%d %d:%d%s %d:%d:%d \n",webDataRec.userName,
webDataRec.logonDate.month,
webDataRec.logonDate.day,
webDataRec.logonDate.year,
iHour,
webDataRec.logonTime.min,
AmPmStr,
webDataRec.duration.hour,
webDataRec.duration.min,
webDataRec.duration.sec
);
cout << outbuff ;
}
infile.close();
}
main()
{
Go();
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
C string libraries are REQUIRED!!! ifstream.getline requires C-style string.. Also C string libraries make parsing easier04/28/21