Monitoring txt file and reading new(last)entry(word) from that txt file

Hello!Another beginner over here.
I need to track changes in my .txt file,and every time new word is added to .txt,my program should put it on screen.By now I was able to write that code(with a little help from google) for putting that last added word to screen,but I don't know how to continously monitor .txt file for new words.I don't want to run my program all over again every time new word is added.
I would really appreciate help!
Thank you all in advance!!!
This is my code 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
   #include <iostream>
   using std::cout;
   #include <fstream>
   using std::ifstream;
   #include <string>
   using std::string;
   using std::getline;
      size_t count_lines(const char *filename)
      {
      ifstream myfile("test.txt");
      string line;
      size_t count = 0;
      while ( getline(myfile, line) )
      {
      ++count;
      }
      return count;
      }
      int main()
      {
      const char filename[] = "test.txt";
      size_t i, count = count_lines(filename);
      ifstream myfile(filename);
      string line;
      for ( i = 0; i < count - 1; ++i )
      {
      getline(myfile, line); 
      }
      while ( getline(myfile, line) )
      {
      cout << line << "\n";
      }
      return 0;
      } 


Following is one way to do it, rudimentary though...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;

int main()
{
	int ch;
	while (true){
		if ((ch = cin.get()) != EOF){
			cout << (char)ch;
		}
		else{
			usleep(3000000);
			cout << "I'm awake..." << endl;
			cin.clear();
		}
	}
}


Please note that I am using the platform specific function usleep to suspend execution for 3 seconds. This may not work if you are running this code on other platforms that does not have this particular function. For instance, in Windows, you should replace this with the sleep API.

Finally, in order to monitor a file, you will run this program as follows (assuming a.out is the name of the program)

$ a.out < myfile.txt
The most obvious solution is to use some sleep function then wake up check then go sleep again etc etc. In Unix, select function may also be viable. Your code remain "sleeping" until the select function return to indicate the file descriptor you have been waiting is ready for more data to read. However select parameters are much more complicated than sleep.

Select function is often used in Unix sockets programming though.
Thank you guys for your help!I really appreciate it!!and especially to -naivnomore- for a example of a code.I work in windows so to use Sleep() function I suposse that I will need #include<windows.h>.
Also,I'm really a beginner,so I have difficulties to intergrate that function in my code.And I'm not shure will my program go back to sleep after first run?or I need to do another loop just for start program again?

Last,what do you all think about using the filesystem change notification API's for this problem?
Yes you can replace usleep function with the Sleep API in windows. However, Sleep's parameter takes milliseconds & not microseconds like usleep. The while(true) loop keeps your program running forever until you hit Ctrl+C on Windows. Therefore, you do not need another loop. I did not look at the link but the very name of the url "directory change notifications" tells me that it might give you some kind of an event whenever a file change occurs. Therefore, that might be a much better option than polling. Good Luck.
Topic archived. No new replies allowed.