I need help with creating a log entry. The split() function is done and working
ID: 3537723 • Letter: I
Question
I need help with creating a log entry. The split() function is done and working program is listed after the question:
This function does the following:
My program so far:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> split(const string &, char);
int main()
{
vector<string> vString;
string str="abc:12345:xx"; //Full string
char split_char=':'; //Split character
cout<<"String Entry: "<<str<<endl<<endl;
cout<<"Split character: "<<split_char<<endl<<endl;
cout<<"Split string: "<<endl;
vString=split(str,split_char); //Split function call
for(int i=0;i<vString.size();i++)
cout<<vString.at(i)<<endl;
cout<<endl;
system("pause");
return 0;
}
Log_Entry create_Log_Entry(const string&)
{
}
vector<string> split(const string &str, char ch)
{
//cin>>str;
//cin>>ch;
vector<string> vString;
string temp="";
for(int i=0;i<str.length();i++)
{
if(str.at(i)!=ch)
temp+=str.at(i);
else
{
vString.push_back(temp);
temp="";
}
}
if(str.at(str.length()-1)!=ch)
vString.push_back(temp);
return vString;
}
Log_Entry create_Log_Entry(const string&); This function does the following:
- The parameter is a log entry line.
- Create a
Log_Entryobject. - Break the line into fields and subfields using
split(). Several splits will be necessary. - Fill the data members with values.
- Split on ' ' (space character) first, if the split does not give 10 strings set the number of bytes to 0 and return. (Some lines in the file may be bad.)
- For
stringtointconversion use string streams. There is a link to C++ examples on my main page where more conversion examples can be found. - Return the
Log_Entryobject.
My program so far:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> split(const string &, char);
int main()
{
vector<string> vString;
string str="abc:12345:xx"; //Full string
char split_char=':'; //Split character
cout<<"String Entry: "<<str<<endl<<endl;
cout<<"Split character: "<<split_char<<endl<<endl;
cout<<"Split string: "<<endl;
vString=split(str,split_char); //Split function call
for(int i=0;i<vString.size();i++)
cout<<vString.at(i)<<endl;
cout<<endl;
system("pause");
return 0;
}
Log_Entry create_Log_Entry(const string&)
{
}
vector<string> split(const string &str, char ch)
{
//cin>>str;
//cin>>ch;
vector<string> vString;
string temp="";
for(int i=0;i<str.length();i++)
{
if(str.at(i)!=ch)
temp+=str.at(i);
else
{
vString.push_back(temp);
temp="";
}
}
if(str.at(str.length()-1)!=ch)
vString.push_back(temp);
return vString;
}
Explanation / Answer
#else
#include <string>
using std::string;
#endif
using namespace std;
vector<Log_Entry> parse(const string&)
{
string line;
vector<Log_Entry> _line;
ifstream myfile ("name.txt");
if(!myfile)
{
cout << "Couldn't open file";
}
if (myfile.is_open())
{
while (! myfile.eof())
{
getline (myfile,line);
Log_Entry _name(line);
_line.push_back(_name);
}
myfile.close();
}
return _line;
}
vector<string> split(const string& entries, char x)
{
string entry;
vector<string> _entry;
int index;
for(int i = 0, j = 0; i < entries.size(); i++)
{
index = entries.find(x,i);
if (index == entries.size())
{
break;
while (j != index)
{
entry += entries[j];
j++;
}
i = i + j;
_entry.push_back(entry);
}
// if(j != 9)
// _entry[9] = 0;
}
return _entry;
}
int byte_count(const vector<Log_Entry> &_name)
{
int bytes = 0;
for (int i = 0; i < _name.size(); i++)
{
bytes += _name[i].get_number_of_bytes();
}
return bytes;
}
void output_hosts(vector<Log_Entry> _name)
{
for(int i = 0; i < _name.size(); i++)
{
string a = _name[i].get_host();
cout << "hostd: " << a[0] << endl;
}
}
Log_Entry::create_Log_Entry(const string&)
{
vector<string> entries = split(entry, ' ');
_host = entries[0];
vector<string> dates = split(entries[3], '/');
_date.day = dates[0];
_date.month = dates[1];
vector<string> times = split(dates[2], ':');
istringstream my(times[0].c_str());
int time = 0;
my >> time;
_date.year = time;
istringstream my2(times[1].c_str());
my2 >> time;
_time.hour = time;
istringstream my3(times[2].c_str());
my3 >> time;
_time.minute = time;
istringstream my4(times[3].c_str());
my4 >> time;
_time.second = time;
_request = entries[5];
_status = entries[8];
istringstream my5(entries[9].c_str());
my5 >> time;
_number_of_bytes = time;
}
void output_all(const vector<Log_Entry> &entries)
{
for(int i = 0; i < entries.size(); i++)
{
cout << "***********************************************" << endl;
cout << "Date Day:" << entries[i].get_date().day << endl;
cout << "Month :" << entries[i].get_date().month << endl;
cout << "Year :" << entries[i].get_date().year << endl;
cout << "Host :" << entries[i].get_host() << endl;
cout << "------------------------------------------------" << endl;
cout << "Time Hours:" << entries[i].get_time().hour << endl;
cout << "Time Min :" << entries[i].get_time().minute << endl;
cout << "Time Sec :" << entries[i].get_time().second << endl;
cout << "Request :" << entries[i].get_request() << endl;
cout << "Status :" << entries[i].get_status() << endl;
cout << "Bytes :" << entries[i].get_number_of_bytes() << endl;
cout << "***********************************************" << endl;
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.