Display a line from text file

Pages: 123
Display a line from text file , my problem is i cant display the lines which only contains the specific date from my main, I appreciate your comments , thankyou


//main

int main(int argc, char* argv[]) {

LogReader l("/home/kaertech/stats_20140801_061314.log");
string log_contents;

//log_contents = l.getLogs();
log_contents = l.getLogs("2014-08-01");

cout << "File content is:" << endl;
cout << log_contents << endl;
}
//header

class LogReader {

public:
string getLogs();
string getLogs(string b);
LogReader(string a);




private:
string fContent;
fstream logFile;
string path;
string date;
};


//function
string LogReader::getLogs(string b){

date = b;


logFile.open(path.c_str(), ios::in);
istringstream isstream (date);

if( logFile.fail()){
cout << "Can't open file" << endl;
}

while(getline (logFile,date) && !logFile.eof()) date.push_back(logFile.get());
{

if(date.find(b) == string::npos)
return date;
}

logFile.close();
return b;

}


//contents of logfile
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
Since there are multiple lines containing the required date, you may want to do something with each of those lines. Here I stored the selected lines in a vector, then displayed them.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

    using namespace std;

class LogReader {

public:
    LogReader(const std::string & pth) : path(pth) { };
    void getLogs(const string & b);
    void display_contents() const;

private:
    string path;
    std::vector<std::string> lines;
};

void LogReader::getLogs(const string & b)
{    
    lines.clear();

    ifstream logFile(path.c_str());
    
    if (logFile.fail())
    {
        cout << "Can't open file" << endl;
        return;
    }

    string line;
    while (getline (logFile,line) )
    {
        if (line.find(b) != string::npos)
            lines.push_back(line);
    }
}

void LogReader::display_contents() const
{
    for (size_t i=0; i<lines.size(); i++)
        cout << lines[i] << endl;
}

int main()
{
    LogReader l("/home/kaertech/stats_20140801_061314.log");

    l.getLogs("2014-08-01");

    cout << "File content is: " << endl;
    l.display_contents();
}
thank you so much, it really worked, ! :D but is it possible to retain my original main function?, because my professor required me not to change the main function.

Of course that's possible. i wasn't sure what requirements or restrictions there were. I suggest you simply borrow some of the ideas from my code which were useful, and discard those parts which you don't need.
ok thankyou again :D
thanks :D


can i ask what's the meaning of this line ?:)
LogReader(const std::string & pth) : path(pth) { };
Last edited on
The defines a constructor for the LogReader class that takes a string as an argument and initializes the member variable "path" to that value.

http://www.cplusplus.com/doc/tutorial/classes/
Specifically:
http://www.cplusplus.com/doc/tutorial/classes/#constructors
http://www.cplusplus.com/doc/tutorial/classes/#member_initialization
THANKS :D
Interesting. There is a differently shaped well known wheel:
grep "^2014-08-01" /home/kaertech/stats_20140801_061314.log

That is naturally not the purpose of that programming exercise.

I did use a different filtering condition though. It makes no difference with the sample data, but nevertheless
you could consider the implications of "contains" vs "starts with".
Hi ! I have updated my code already but i still have unfinished requirements

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

LOGFILE 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


REQUIRED OUTPUT SHOULD BE:

1.ALL CONTENTS
2. LINES WHICH CONTAINS THE DATE 2014-08-01
3. LINES FROM 2014-08-01 TO 2014-08-10

so far this is my codes, I appreciate your reccomendations and revisions :)

cpp.sh/5tt


x
Last edited on
Use the code tags, please.

RAII. Read the file only once, in constructor. Store in clever way.

Make each get* write to stringstream, then return as string.
Hi ! I have updated my code already but i still have unfinished requirements

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

LOGFILE 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


REQUIRED OUTPUT SHOULD BE:

1.ALL CONTENTS
2. LINES WHICH CONTAINS THE DATE 2014-08-01
3. LINES FROM 2014-08-01 TO 2014-08-10

so far this is my codes, I appreciate your reccomendations and revisions :)

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
//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 & a);
string getLogs(const string & b, const string & c);


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 & a)
{


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;

}*/
Last edited on
RAII. Read the file only once, in constructor. Store in clever way.

Make each get* write to stringstream, then return as string.
how can i possibly return this function to string ?

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
//main
int main(int argc, char* argv[]) {

	LReader l("/home/kaertech/stats_20140801_061314.log");
	string contents;

	std::string strFilterDate = "2014-08-01";
	contents = l.getLogs(strFilterDate);
	std::cout << "File content is: " << std::endl;
	std::cout << contents << std::endl;

}

//LReader.cpp
string LReader::getLogs(const string & b){
	ifstream logFile(path.c_str());

	    if (logFile.fail())
	    {
	        cout << "Can't open file" << endl;
	        return 0;
	    }

	   string line;
	    while (getline (logFile,line) )
	    {
	        if (line.find(b) != string::npos)
	            lines.push_back(line);
	    }

	    for (size_t i=0; i<lines.size(); i++)
	    	cout << lines[i] << endl;

	   return b (lines);

}


//header

class LReader {

	public:
	LReader(const string & pth) : path(pth) { };



	string getLogs(const string & b);

	private:
	string fContent;
	fstream logFile;
	string path;
	vector<string> lines;




};


whats wrong?
my output is always like this:

2014-08-01 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
File content is:

PROBLEM: "File Content is" should shown BEFORE the datas will be shown, my main function is permanent, i cant change it, WHAT WILL i DO?
See std::ostringstream
How can I get the lines from date start-"2014-08-01" to DateEnd = "2014-08-10"; This is my code so far, Can you suggest that is more applicable, because i get errors, thnkyou




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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) )
		   {

			  return std::find(c.begin() , d.end(), d);


		   }



}
what does my "x" variable contains?
Last edited on
A string, obviously. What string derived from a line2 would give desired results with those relational operators?
Pages: 123