Hi, I need to search for a key word in last n/100 lines of a text file. Here is the code i have so far. This search whole file & that is an issue. This is not a home work question, please help.

ifstream readFile(fileName);//read the file 

    while( readFile >> word )
    {
      if ( word == badCode )
      {
        print("Found word %s\n", badCode);
        return true;//string found
      }
    }

First, you need a way to determine the last 100 lines. getline with a fixed-size buffer sounds like a start there.
Then you iterate over only the lines left in the buffer.

My C++ isnt so good. Little bit more help would be very nice.

getline will read in an entire line into a string value. You can use any of the standard containers to store the lines, but I'd probably just use a vector. The vector has plublic methods for checking it's size, so your code might be structured like

vector< string >
while ( input has lines available ) 
   read a line
   append line to vector
   if vector size is > 100
      remove one element from the front
   end
end while
for all elements in the vector
   check for keyword
end fo

A rolling window would be the simplest solution, in my opinion, at least to get the last N lines:

#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

using namespace std;

namespace {
    const size_t N_LINES = 100;
}

int main()
{
    ifstream in("filename");
    
    if (in) {
        list<string> wind;
        string line;

        while (getline(in, line)) {
            wind.push_back(line);

            if (wind.size() > N_LINES)
                wind.pop_front();
        }

        cout << "Last " << wind.size() << " lines:\n";
        copy(wind.begin(), wind.end(), ostream_iterator<string>(cout, "\n"));
    }
}

Wouldn't that be

copy(wind.begin(), wind.end(), ostream_iterator<string>(cout, '\n'));

?

Narue, you are a life saver. How do I search for a key word in this? Lets say i am looking for the phrase "key word" in last 100 lines of the file. If I find the phrase, I want to print a message.

Also, is it possible for you to include couple of comments, in the code, so i can understand this.


A rolling window would be the simplest solution, in my opinion, at least to get the last N lines:

#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

using namespace std;

namespace {
    const size_t N_LINES = 100;
}

int main()
{
    ifstream in("filename");
    
    if (in) {
        list<string> wind;
        string line;

        while (getline(in, line)) {
            wind.push_back(line);

            if (wind.size() > N_LINES)
                wind.pop_front();
        }

        cout << "Last " << wind.size() << " lines:\n";
        copy(wind.begin(), wind.end(), ostream_iterator<string>(cout, "\n"));
    }
}

Wouldn't that be

copy(wind.begin(), wind.end(), ostream_iterator<string>(cout, '\n'));

?

No.

How do I search for a key word in this?

Well, depending on how thorough you want to be it could be as simple as calling the find() method of the string for each line. However, that's an absolute search rather than a "word" search, so you'll get false positives if the word is embedded in another word. However, start with the find() method, just because an iterative approach is easier to manage. :)

Also, is it possible for you to include couple of comments, in the code, so i can understand this.

I generally don't provide comments unless there's something especially obtuse that needs explaining. The idea is that you shouldn't be handed everything, and trying to understand someone else's code can be enlightening. I'm not cruel though, so if you just can't figure out a part of the code, just ask and I'll explain it.

I am still stuck in this. Following part of the code does not print anything at all. I am running this on HPUX 11.23 and Ubuntu. Because of this, I can not verify if I have actually read last 100 lines of my log file. I should mention that my log file is REALLY big.

cout << "Last " << wind.size() << " lines:\n";
copy(wind.begin(), wind.end(), ostream_iterator<string>(cout, "\n"));

Also the phrase I am trying to search is a char string. It is declared as follows.

char searchForCode[STRING_MAX];

If I remember correct, i can not use char string in find(), right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.