C C C get the text file weblogtxt httpwwwsantarosaedulmeadew

C++ C++ C++

get the text file weblog.txt (http://www.santarosa.edu/~lmeade/weblog.txt)

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 to the 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 (http://www.santarosa.edu/~lmeade/weblog.html)

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://httpd.apache.org/docs/2.2/logs.html

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


•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 . Must declare the vector in a function.


•Create another non-member function to sort the contents of the vector. Must pass the vector by reference!


•Create another non-member function that will accept an integer line number in the parameter list and string return value. When the function is called in main, a line number must be passed to the function. The function should then return the line from the vector that corresponds to that line number. The first line in the file will be line 1. You must pull the line from the vector, not the file. Because you are required to pull the line from the vector, you must include the vector in the parameter list of this function and pass the vector to this function when it is called by main. The main function should then display the string that is returned by the function.


•Create one more non-member function to write the contents of the vector to a file. Again, you will need to pass the vector by reference to this function. Each element of the vector should be on its own line in the new file.   The contents of the new file should look like the original input file once your program completes, but in sorted order . You are not required to write a sort function. Instead, #include < algorithm > and use the sort function.

Here\'s a quick info:
Beginner\'s Guide to the sort Function (http://www.cplusplus.com/articles/NhA0RXSz/)

•Create a main function that calls all of your non-member functions .

Solution

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

std::vector<string> readFileAndWriteToVector();
void sortVector(std::vector<string> &vec);
string getLine(int i , std::vector<string> &vec);
void writeVector(std::vector<string> &vec);

int main () {

std::vector<string> vec = readFileAndWriteToVector();

vector<string>::iterator v = vec.begin();
while( v != vec.end()) {
cout << \"value of v = \" << *v << endl;
v++;
}

sortVector(vec);

string line1 = getLine(2, vec);

cout << \"String of v = \" << line1 << endl;

writeVector(vec);

vector<string>::iterator v2 = vec.begin();
while( v2 != vec.end()) {
cout << \"value of v = \" << *v2 << endl;
v2++;
}
}

std::vector<string> readFileAndWriteToVector(){
string line;
vector<string> vec;

ifstream myfile (\"logs.txt\");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
vec.push_back(line);
}
myfile.close();
}

else cout << \"Unable to open file\";

return vec;

}

string getLine(int i , std::vector<string> &arr){
return arr.at(i-1);
}

void sortVector(std::vector<string> &vect){
std::sort (vect.begin(), vect.end());
}


void writeVector(std::vector<string> &vec){
ofstream myfile;
myfile.open (\"web log2.txt\");
vector<string>::iterator v = vec.begin();
while( v != vec.end()) {
myfile << *v << \"\ \";
v++;
}
myfile.close();
}

C++ C++ C++ get the text file weblog.txt (http://www.santarosa.edu/~lmeade/weblog.txt) This file is an Apache web log taken from the web server for St. Mary\'s
C++ C++ C++ get the text file weblog.txt (http://www.santarosa.edu/~lmeade/weblog.txt) This file is an Apache web log taken from the web server for St. Mary\'s
C++ C++ C++ get the text file weblog.txt (http://www.santarosa.edu/~lmeade/weblog.txt) This file is an Apache web log taken from the web server for St. Mary\'s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site