Vectors and Reading/Writing Files. Help!!!

I am having so much trouble with this! I can't figure out how to write the code for this with the following requirements:

download the text file 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.

Create a non-member function to read each line of the web log file. This function must do error checking to ensure that the file is opened successfully, otherwise it must provide a message like "file not available" to the user.

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. Note: You must declare the vector in a function.

Create another non-member function to sort the contents of the vector. Make sure to pass the vector by reference or your sort will disappear when the function ends! Use the sort function with #include <algorithm> to do the sort; you do not have to write your own sort algorithm.

Create one more non-member function to write the contents of the vector to a file. Each element 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.

Create a main function that calls all of your non-member functions.
This is what I have so far:
#ifndef __Input_Output__InputOutput__
#define __Input_Output__InputOutput__

#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class webstuff
{
public:
void ReadFile();
void WriteFile();
};
void webstuff::ReadFile()
{
ofstream myfile("weblog.txt");
if (myfile.is_open())
{
cout << "File was opened." << endl;
}
else cout << "Error. File was not opened." << endl;
vector <string> readLines;
readLines.push_back("weblog.txt");
}
void webstuff::WriteFile()
{

}

I use Xcode btw
Use code blocks when you paste code (click the <> button to the right).

As far as I know, you need to read the data into a temporary string before you can use push_back to store it in your array.

1
2
3
4
vector <string> readLines;
string temp;
while ( getline ( myfile, temp ) )
    readLines.push_back ( temp );
Topic archived. No new replies allowed.