Hello - I need to create a utility using C/C++ to parse information from a XML audit log file that is continuously being appended by a separate process

I need to read the information in and process it, then when I reach the EOF I need to wait/monitor for more content to be added to the file and read/process the new information.

The code snippet below shows my initial approach -- the issue is that once the EOF is reached and it goes into the sleep loop, it does not pick up any additions to the file.

I did find a posting about this topic on the Java board, but it's old -- and I'm much less savvy with C/C++ than I am with Java.

Any suggestions would be appreciated :)

while(true){
	if(inputStream.read(&mChar,1)) { // I tried using getline() here too...
		// read the characters
	} else {
		// backup over the EOF character - not sure if this is necessary...
		inputStream.unget(); // I tried doing a putback() here too...
		sleep(10);
	}
// run until interrupt is caught by sighandler...
}

While reading the source code for the Linux 'tail' utility, I found that the way they handle the -f option is what I needed... I had to use fopen() and fgetc() to do what I need -- the trick is the fflush() to reset the counters.

FILE* auditFile;
	// get the file
	int charInt = 0;
	while (true){
		charInt = fgetc(auditFile);
		if(charInt != EOF){
			std::cout << (char)charInt;
		} else {
			fflush(auditFile);
			sleep(1);
		}
	}
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.