1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//#include "test_header.h"
struct Login
{
std::string user_id;
std::string ip_address;
std::string login_month;
int login_day;
int login_duration; //Minutes
Login(const std::string& id, const std::string& address, const std::string& month, const int day, const int duration)
: user_id(id), ip_address(address), login_month (month), login_day (day), login_duration(duration){}
};
std::ostream& operator<< ( std::ostream& out, const Login& b) //Overload << to print out parsed data
{
out << b.login_month << " " << b.login_day << " " << b.ip_address << " " << b.login_duration << std::endl;
return out; //Return out to keep lh operand as ostream, so all params pass (cout << login1 << login2)
}//End operator<< overload
int main() //Take userid as param
{
std::vector<Login> logins{};
std::ifstream inFile{"D:\\test.txt"};
std::string startInfo{};
if(inFile)
{
std::string dummy{};
getline(inFile, dummy);//read and discard first line
std::string line{};
while (getline(inFile, line) )
{
if(line.empty())break;//breaks loop on penultimate line
std::istringstream stream{line};
std::string name {}; std::string address{}; std::string month{};
int day{}; int duration;
while (stream)
{
stream >> name;
stream >> dummy;//pts
stream >> address;
stream >> dummy;//day of the week
stream >> month;
stream >> day;
stream >> dummy;//start time -
stream >> dummy;// character -
stream >> dummy; // end time
std::string timePeriod{};
stream >> timePeriod;
if(timePeriod.size())
timePeriod = std::string{timePeriod.begin()+1, -- timePeriod.end()};//remove '(' and ')'
std::istringstream durationStream{timePeriod};
std::string durationString{};
while (durationStream)
{
getline(durationStream, dummy, ':')
//removed '00: - check this in case duration > 1 hour (or 99 mins?)
&& getline(durationStream, durationString);
}
std::istringstream timeStream{durationString};
timeStream >> duration;
}
if(inFile)
{
logins.emplace_back(Login(name, address, month, day, duration));
}
}
getline(inFile, startInfo);//last line
}
for (const auto& elem : logins)std::cout << elem ;
std::cout << startInfo << "\n";
}
|