yes sir I need to read every line from the file, parse it to extract the date field, and then check whether that date is within the specified range, could you advice or suggest applicable codes than this? I want to display the datas from
"2014-08-01" to "2014-08-10"
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
|
//main
int main(int argc, char* argv[]) {
LReader l("/home/kaertech/stats_20140801_061314.log");
std::string contents;
//
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;
}
//header
class LReader {
public:
LReader(const std::string & pth) : path(pth) { };
std::string getLogs();
std::string getLogs (const std::string & c, const std::string & d);
private:
std::fstream logFile;
std::string path;
};
LReader.cpp
std::string LReader::getLogs (const std::string & c, const std::string & d){
std::ifstream logFile(path.c_str());
std::string result ="";
if (logFile.fail())
{
std:: cout << "Can't open file" << std::endl;
void exit ();
}
std::string line2;
const size_t Max (7);
size_t count (0);
while ( count < Max && getline( logFile, line2 ) )
{
const auto date (result(line2));
if ( c <= date && date <= d ) {
++count;
line2.append(result);
}
}
return result;
//my previous code
std::string LReader::getLogs (const std::string & c, const std::string & d){
std::ifstream logFile(path.c_str());
std::string result ="";
if (logFile.fail())
{
std:: cout << "Can't open file" << std::endl;
void exit ();
}
std::string line2;
while (getline (logFile,line2))
{
std::size_t f1 = line2.find(c);
std::size_t f2 =line2.find(d);
if (f1!=std::string::npos)
f1++;
if ( f2!=std::string::npos)
break;
std::cout << line2 << std::endl;
}
return line2;
}
|
logfile contens:
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-02 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-03 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-05 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-07 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-09 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 07:13:14,Name,4.5,CustomUnit,CustomType
I only want to display the BOLD LINES