Okay I am writting a program that will take input from a file and store it in a string. Then the string is separated at the whitespaces and these separate strings are loaded in a vector. My problem is that how would I index the vector so that the strings I input can be put in a class?
okay I have a .h file that looks like this......
i also have a .cpp file that looks like this
this .cpp file will open a txt file and input lines on info one at a time. I am now trying to split the string up and load it in a vector.
okay I have a .h file that looks like this......
Code:
// struct, all members are public.
struct Date
{
Date()
: year(0) {};
string day;
string month;
int year;
};
// struct, all members are public.
struct Time
{
Time()
: hour(0), minute(0), second(0) {};
int hour;
int minute;
int second;
};
// Only the Log_Entry constructor is not implemented.
class Log_Entry
{
public:
Log_Entry(string);
string get_host() const { return _host; }
Date get_date() const { return _date; }
Time get_time() const { return _time; }
string get_request() const { return _request; }
string get_status() const { return _status; }
int get_number_of_bytes() const { return _number_of_bytes; }
private:
string _host;
Date _date;
Time _time;
string _request;
string _status;
int _number_of_bytes;
};
std::vector<string> split (const string&, char);
std::vector<Log_Entry> parse(string);
void output_all(const std::vector<Log_Entry> &);
int byte_count(const std::vector<Log_Entry> &);
void output_hosts(std::vector<Log_Entry>);
#endif
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Comment