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
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// void print_vector_index(){
// int a;
// cout << \"Which line You want to see\" << endl;
// cin<<a;
// cout <<
// }
int main()
{
int a;
std::string line;
std::vector<std::string> DataArray;
// Enter the file name you want to read
std::ifstream myfile(\"FileName\");
if(!myfile) //Always test the file open.
{
std::cout<<\"Error opening output file\"<< std::endl;
//system(\"pause\");
return -1;
}
//read each line of the file
while (std::getline(myfile, line))
{
// Store the data in the vector
DataArray.push_back(line);
}
// Sort the each element of the vector
for (int i=0 ; i<DataArray.size();i++){
std::sort(DataArray[i].begin(), DataArray[i].end());
}
// write the result vector to a file
// Please write the name of the file
std::ofstream output_file(\"FileName_to_write_on\");
std::ostream_iterator<std::string> output_iterator(output_file, \"\ \");
std::copy(DataArray.begin(), DataArray.end(), output_iterator);
cout << \"Which line You want to see\" << endl;
cin >> a;
// Print the required line
cout << DataArray[a];
return 0;
}

