Comparing and Parsing Data from text file

Chervil
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



Last edited on
When i had a go at writing the code for this, i used the substring member function of the string class.

In this case, the string is line2. You just need to use substr() to get the first 10 characters as a separate string. Take a look at the sample code here:
http://www.cplusplus.com/reference/string/string/substr/

In particular look at how the word "generalities" is obtained from the sentence.

(I also notice you still have the incorrect code void exit (); which I pointed out yesterday.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
#include <regex>

int main( void )
{
  std::ifstream input( "input.txt" );
  std::string line;
  std::regex date( "^(2014-08-((0[1-9])|(10)))(.*)" );

  const int limit = 10;
  int count = 0;

  while( getline( input, line ) && count < limit)
  {
    if ( std::regex_match( line, date ) )
    {
      ++count;
      std::cout << line << '\n';
    }
  }
  return 0;
}


Or, since we are in a unix board:
1
2
3
4
5
6
7
#include <iostream>

int main( void )
{
  system( "grep \"2014-08-\\(0[1-9]\\)\\|\\(10\\).*\" input.txt | head -n 10" );
  return 0;
}
Last edited on
I used substr() but it seems that there is an error in finding d, my output still shows the entire contents of the textfile @Chervil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::string LReader::getLogs (const std::string & c, const std::string & d){

	std::ifstream logFile(path.c_str());

    std::string line2;



    while (getline (logFile, line2)){

    	if (line2.find(c)    != std::string::npos && line2.size() < 9)
    	         std::cout << line2.substr(9) <<std:: endl;

    	if (line2.find(d)    != std::string::npos && line2.size() < 9)
    	         std::cout << line2.substr(9) <<std:: endl;

    	else
    		std::cout << line2 << std::endl;




    }
}
Last edited on
@Lowest0ne i also tried the code you suggested, but there's an error , it shows like this, thanks for suggesting by the way

 error: ‘system’ was not declared in this scope
I have updated my code again but the problem is my output display the line with even values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::string LReader::getLogs (const std::string & c, const std::string & d){

	 std::ifstream logFile(path.c_str());

     std::string line2;
     while (getline (logFile, line2)){
    	 
	 std::size_t f1 = line2.find(c);
	 std::size_t f2 =line2.find(d);
    	int i;

    	for (i = 0; i < line2.length(); ++i){

    		if (f1 <= line2 [i] && line2[i] <= f2) break;
    	}

      std::string start= line2.substr(i);
      getline( logFile, start);
      std::cout << start << std::endl;


 }

}



2014-08-02 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-04  06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-06  06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-08  06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-10  06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02  07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02  07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02  07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02  07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02  07:13:14,Name,4.5,CustomUnit,CustomType



I appreciate your help, thanks
The expected solution does not use find() at all.
Ugh what happened to this thread? http://www.cplusplus.com/forum/unices/140803/3/ We gave you the information needed to finish this.
I bet on two things for "what happened":
1. According to George Bernard Shaw we have mere illusion that we have communicated.
2. The OP has started numerous threads on this topic.

Repetitio est mater studiorum (occasionally), so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string LReader::getLogs( const string & first, const string & last )
{
  string result {""};
  ifstream logFile( path );
  if ( ! logFile ) {
    cerr << "Can't open file " << path << '\n';
    return result;
  }
  string line2;
  const size_t Max { #1# };
  size_t count {0};
  while ( count < Max && getline( logFile, line2 ) )
  {
    const string date { #2# };
    if ( first <= date && date <= last ) {
      ++count;
      #3#
    }
  }
  return result;
}

There are now three tags that you have to replace with real code:
#1# How many lines do you want?
#2# This is where you should use substr.
#3# Append line2 to result.
HI @keskiverto sorry for replying late, i've tested the codes you gave me,
1
2
3
4
5
6
7
8
9
10
11
12
const size_t Max = 10;
    std::size_t count ( 0);
    while (count < Max && getline (logFile, line2))
    {
    	const std::string date ( line2.substr (count));
    	if (c<= date && date <= d){
    		++count;
    	  res.append(line2);

    	}

    }


i have problems with line 5,can you please tell me if i used substr in a correct way? thankyou my output displays only the last line

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


I can tell you that you did not use it the right way.

You have been given this link multiple times: http://www.cplusplus.com/reference/string/string/substr/
Follow it and read.
Topic archived. No new replies allowed.