The text file http://www.santarosa.edu/%7Elmeade/weblog.txt You should be comfor
ID: 3574567 • Letter: T
Question
The text file http://www.santarosa.edu/%7Elmeade/weblog.txtYou should be comfortable with the content in the modules up to and including the module "Input Output" for this project. You must follow the style guide e for all projects. For this project, download the text file weblog.txt e Note: To download this file, right click on the link and select SAVE AS This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request tolthe web server to view a web page, which is a file residing on the server. Each time a page is requested by a browser, the web server records information about that request. This weblog.txt holds the details of some of those requests. See below for a list of fields with an example: Web Log Example e This file does not include all possible information that could be collected by a web server. The full description of the apache log file format can be found here: http:/httpdapache.org/docs/2.2logs.htmle For this project, you can use each line of the web log file as one string using the string class getline function Minimum Requirements: Create a non-member function to read each line of the web log file (3 points. Each line should then be stored in a vector such that the first element of the vector is the first line of the web log file. Because each element will hold an entire line from the file the vector should be declared as a vector of strings 3
Explanation / Answer
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <fstream>
#include <iterator>
using namespace std;
int main () {
vector<string> v;
v= readStore();
do_sorting(v);
string s= readByLine(5,v);
cout<< s;
sortWrite(v);
return 0;
}
//reading from weblog.txt line by line and storing it to vector
vector readStore(){
std::ifstream file("weblog.txt");
std::string str;
vector<string> lines;
while (std::getline(file, str))
{
lines.push_back(str);
}
return lines;
}
//sorting vector
void do_sorting( std::vector<string> &lines){
// Sorting the string vector
sort(lines.begin(), lines.end());
}
//Reading the string stored at given line number
string readByLine(int n,vector<string> lines){
return lines[n];
}
// sorting given vector and writing to weblog1.txt to create a file similar to weblog.txt
void sortWrite(std::vector<string> &lines){
// Sorting the string vector
sort(lines.begin(), lines.end());
std::ofstream output_file("./weblog1.txt");
std::ostream_iterator<std::string> output_iterator(output_file, " ");
std::copy(lines.begin(), lines.end(), output_iterator);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.