Requirements in filtering the text file. GRATEFUL TO HAVE YOUR SUGGESTIONS
1. first my professor required me NOT to change the MAIN function(because he made it)
2. I have to make 3 getlogs() STRING FUNCTIONS:
a. string getlogs(); - accepts no paramters, SHOWS ALL THE CONTENTS OF TEXT FILE
b. string getLogs(const string & a); - accepts 1 parameter -SHOWS ONLY THE LINE WHICH CONTAINS THE SPECIFIED DATE FROM MAIN FUNCTION which is "2014-08-01"
c. string getLogs(const string & b, const string & c); - accepts 2 parameters, SHOWS ONLY THE LINES FROM THE DATE START to DATE END specified at THE MAIN FUNCTION which is date start-"2014-08-01";DateEnd = "2014-08-10";
3. all COUT should be in the MAIN FUNCTION
TEXTFILE CONTAINS:
2014-08-01 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-03 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-04 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-05 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-06 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-07 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-08 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-09 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-10 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-11 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-03 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-05 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-07 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-09 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-10 07:13:14,Name,4.5,CustomUnit,CustomType
my codes so far:
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//MAIN
#include <iostream>
#include <string>
#include <fstream>
#include <dirent.h>
#include "LogReader.h"
int main(int argc, char* argv[]) {
string contents;
LogReader l("/home/kaertech/stats_20140801_061314.log");
// test first getLogs() no parameters
contents = l.getLogs();
std::cout << "File content is: " << std::endl;
std::cout << contents << std::endl;
// test 2nd getLogs function which accepts 1 parameter which shows the line containing the specified date only
std::string strFilterDate = "2014-08-01";
contents = l.getLogs(strFilterDate);
std::cout << "File content is: " << std::endl;
std::cout << contents << std::endl;
// test 3rd getLogs function accepts 2 parameters that shows the line from the date start up to date end
std::string strDateStart = "2014-08-01";
std::string strDateEnd = "2014-08-10";
contents = l.getLogs(strDateStart, strDateEnd);
std::cout << "File content is: " << std::endl;
std::cout << contents << std::endl;
}
//LogReader.h
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <unistd.h>
using namespace std;
#ifndef LOGREADER_H_
#define LOGREADER_H_
class LogReader {
public:
LogReader(const string & pth) : path(pth) { };
string getLogs();
string getLogs(const string & b);
string getLogs(const string & c, const string & d);
private:
string path;
string fContent;
vector<string> lines;
};
#endif /* LOGREADER_H_ */
//LogReader.cpp
/*
* LogReader.cpp
*
* Created on: Aug 18, 2014
* Author: geraldyne
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <dirent.h>
#include <sstream>
#include "LogReader.h"
#include <unistd.h>
using namespace std;
string LogReader::getLogs(const string & b)
{
ifstream logFile(path.c_str());
if (logFile.fail())
{
cout << "Can't open file" << endl;
return;
}
while (getline (logFile,fContent) )
{
if (fContent.find(b) != string::npos)
lines.push_back(fContent);
for (size_t i=0; i<lines.size(); i++)
cout << lines[i] << endl;
}
}
/*i can only use cout at the main function to show the output which is the STRING CONTENTS from main
void LogReader::display_contents() const
{
for (size_t i=0; i<lines.size(); i++)
cout << lines[i] << endl;
}*/
string LogReader::getLogs (const string & c, const string & d){
ifstream logFile(path.c_str());
if (logFile.fail())
{
cout << "Can't open file" << endl;
void exit ();
}
string line2;
while (getline (logFile,line2) )
{
if (line2.find(c.begin()) = string::npos);
}
|