At last this is it, 2 weeks late but I've added 2 weeks to the deadline
Design, write and test a program in C++ to calculate total usage times per user from a file of login and logout records. Every time a user logs into a Unix workstation or server the login and subsequent log-out are recorded by single records in a logfile. Periodically (every few days or months) administrators might print a summary of the contents of the ever-growing logfile. The coursework is a program to produce such a summary from a simplified version of the logfile.
Each record (line) in the file has 4 fields: (i) the terminal or network line on which a login occurs, (ii) the user name, (iii) the time of the login expressed as the number of seconds since the 1st January 1970, and (iv) the name of the remote system that the user logged in from, this is only used for log in records. There are four different sorts of record:
~'' (tilde), the remote system
name will be ``~'',
~'' (tilde); the remote system
name will be ``~''.
Any logouts without corresponding logins should be reported as errors, as should multiple logins on the same line without an intervening logout. When the file has been read the output should be a report the accumulated times in hours and minutes for each user, followed by a list of all the different remote systems each user has logged in from. As an example, given the input file of login and logout records:
tty4 bob 807144000 wink.feis.herts.ac.uk
tty2 root 807145199 localhost
tty1 dumble 807145220 localhost
tty3 polly 807145371 sally.dyn.dhs.org
tty2 ~ 807145396 ~
tty2 bob 807145407 localhost
tty4 ~ 807147600 ~
tty2 ~ 807148006 ~
tty3 ~ 807148016 ~
tty1 ~ 807148024 ~
~ shutdown 807148033 ~
tty1 bob 807229953 tink.feis.herts.ac.uk
tty1 ~ 807232512 ~
~ shutdown 807232520 ~
tty1 bob 808521163 localhost
~ shutdown 808531067 ~
tty1 bob 813449411 wink.feis.herts.ac.uk
tty2 root 813451675 localhost
tty3 polly 813451684 sally.dyn.dhs.org
tty4 dumble 813451881 sally.dyn.dhs.org
tty4 ~ 813451882 ~
tty4 dumble 813451937 sally.dyn.dhs.org
tty2 ~ 813452021 ~
tty3 ~ 813452026 ~
tty4 ~ 813452031 ~
possible program output could be:
User totals: ============= root 0:9 bob 5:54 polly 0:49 dumble 0:48 Remote systems used: =================== bob wink.feis.herts.ac.uk root localhost dumble localhost polly sally.dyn.dhs.org bob localhost bob tink.feis.herts.ac.uk dumble sally.dyn.dhs.orgNB (i) the time for bob might be different depending on how you deal with users still logged in at the end of the input file, and (ii) the table of remote systems used just lists each remote system used by each user, so although root logged in from localhost twice it is only reported once.
Hand in:
The assessment will be based on:
In order to open and read the data file you will need to read the section of the C++ notes on file I/O. But here is an example that will read one line of the file at a time on each loop iteration:
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
string line, name, remotesys;
unsigned int time;
infile.open("logrecs.dat");
while(infile >> line >> name >> time >> remotesys) {
cout << line << " " << name << " "
<< time << " " << remotesys << "\n";
}
}
Hint: this is an adequate framework structure for the
top-level of the application, just replace the output
statement by a nested if to select which
operations to invoke on the main objects, for login-tracking,
toylas and remote systems. The best way to process the file
is to deal with the input record by record (ie. don't read and store the
whole lot and process them at the end).
After the loop has processed the input, just invoke operations in
the objects to print the results.
© University of Hertfordshire Higher Education Corporation (1998)