Display a line from text file

Pages: 123
Your function should select lines that are in range [first,last], where the first and last are 10-character lexicographically sortable strings.

Whose line 14 and 9?
thats why find function i think should be used
the last code you posted

1
2
3
4
5
6
7
8
9
10
11
string line2;
  const size_t Max {7};
  size_t count {0};
  while ( count < Max && getline( logFile, line2 ) ) // #2
  {
    const auto date { /*foo(line2)*/ }; // #3
    if ( first <= date && date <= last ) {
      ++count;
      // append line2 to result // #4
    }
  }

No, the find should not. You would have to find every date in the range of [first, last] which would be a lot more work than checking if the date is within those 2 dates.

Check out these links for getting the date.
http://www.cplusplus.com/reference/string/string/substr/
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
http://www.cplusplus.com/reference/istream/istream/getline/

Any of them will work. Since the date has a space after it.

Then either of these for comparing (they do the same thing since the one calls the other anyways).
http://www.cplusplus.com/reference/string/string/compare/
http://www.cplusplus.com/reference/string/string/operators/

I feel like anymore help and I might as well just give you the assignment.

If you are confused on line 9 then you clearly copy/pasted my code earlier...You can use either of these 2

http://www.cplusplus.com/reference/string/string/operator+=/
http://www.cplusplus.com/reference/string/string/append/
Last edited on
1
2
3
4
5
 const auto date { /*foo(line2)*/ }; // #3
	     if (c <= date && date <= d) {
	       ++count;
	       line2.append(result);


line 1 confuses me
If you are comparing c and d to date then they should be the same type(std::string). http://www.cplusplus.com/doc/tutorial/variables/ look at auto towards the bottom.

I really suggest you read the tutorials on this site, learncpp's tutorials and parashifts tutorials.

http://www.cplusplus.com/doc/tutorial/
http://www.learncpp.com/
http://www.parashift.com/c++-faq/
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
std::string LReader::getLogs (const std::string & c, const std::string & d){

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


 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;

}

output:


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
File content is: 
2014-08-10 06:13:14,Name,4.5,CustomUnit,CustomType



what's wrong with my program?
Last edited on
The only thing you did was add a return statement on line 26. I am beginning to think you are just ignoring me and keskiverto so I think it might be time to bow out, good luck.
Tried what you suggested

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::string LReader::getLogs (const std::string & c, const std::string & d){

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

std::string output = "";


 if (logFile.fail())
	 	    {
		 std:: cout << "Can't open file" << std::endl;
	 	        void exit ();
	 	    }
	 std::string line2;

	while(std::getline(logFile, line2) line2.find(d) == std::string::npos)
{
    output += line2;
}
output += line2;
return output;
}



but I get this as an output:

File content is: 
2014-08-10 06:13:14,Name,4.5,CustomUnit,CustomType
Last edited on
pleaase I really need this to work
That code has errors, one of which means it won't compile.
line 11, void exit (); - that is a declaration. What is needed is to call the function like this: exit (1);

line 15,
 
    while(std::getline(logFile, line2) line2.find(d) == std::string::npos)
the condition contains two separate statements, they should be separated by some sort of operator, if you want it to compile.
(though I'd question the relevance of the second statement placed there in any case).

I think you 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, don't you?

(Apologies, I've not been following this thread, I may have lost the plot entirely, please correct me if I'm wrong).
geng07 wrote:
1
2
3
4
5
6
const auto date { /*foo(line2)*/ }; // #3
if (c <= date && date <= d)
{
  ++count;
  line2.append(result);
}

line 1 confuses me

Which part of it?

The auto is C++ keyword that means that the type of variable date is same as the type of the initializing expression. In this case it should be std::string.

I wrote:
#3 You have to figure out what expression the /*foo(line2)*/ should be.

Do not write /*foo(line2)*/. Write something that returns a string. A string that you create from the value of line2. A string that compares favorably relative to the two limit date strings.

We have told you to look at the substr member function of std::string. We have told you that a word like "1321-12-31" has 10 characters. We have hinted that the first 10 characters of each line in your sample inputs have contained similar words.

If you need to write a functional program, then you have to read and think.
Topic archived. No new replies allowed.
Pages: 123