I need to display the first ten lines in this text file from date start which is 2014-08-01 to 2014-08-10
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-02 07:13:14,Name,4.5,CustomUnit,CustomType
These are 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
|
//main
#include <iostream>
#include <string>
#include <fstream>
#include <dirent.h>
#include "LReader.h"
int main(int argc, char* argv[]) {
// test getLogs()
std::string strDateStart = "2014-08-02";
std::string strDateEnd = "2014-08-10";
contents = l.getLogs(strDateStart, strDateEnd);
std::cout << "File content is: " << std::endl;
std::cout << contents << std::endl;
}
//LReader.h
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <unistd.h>
using namespace std;
#ifndef LOGREADER_H_
#define LOGREADER_H_
class LReader {
public:
LReader(const string & pth) : path(pth) { };
string getLogs (const string & c, const string & d);
private:
string fContent;
fstream logFile;
string path;
vector<string> lines;
};
#endif /* LOGREADER_H_ */
//LReader.cpp
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <dirent.h>
#include <sstream>
#include <errno.h>
#include "LReader.h"
#include <unistd.h>
using namespace std;
string LReader::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) != string::npos)
lines2.push_back(line2);
}
// this code below display the last 10 lines, but I need the FIRST 10 LINES
/*for ( std::string line2; std::getline(logFile,line2); )
{
lines2.push_back(line2);
}
for ( std::size_t i = std::min(lines2.size(), std::size_t(5)); i < lines2.size(); ++i )
{
std::cout << lines2[i] << std::endl;
}*/
}
|