Read a file to the end, new data is appended, no more is read?

Gary Willoughby dev at kalekold.net
Thu Jun 20 07:10:55 PDT 2013


I have a simple problem, I want to open a file and read data from 
it. When the file changes, i will read more data. The trouble is 
i only want to open the file once.

Here's the simplified code:

import core.thread;
import std.stdio;

void main(string[] args)
{
	auto file = File("file.txt", "r");

	string line;

	writeln("First Read:");

	while ((line = file.readln()) !is null)
	{
		write(line);
	}

	Thread.sleep(dur!("seconds")(5)); // <-- More data is appended 
to the file while waiting here.

	writeln("Second Read:");

	while ((line = file.readln()) !is null) // <-- Error! New data 
is not read!
	{
		write(line);
	}
}

I read the file, then when its paused, i add new lines to the 
opened file. When the program resumes it totally ignores the new 
lines. doh!

I guess this is the result of buffering somewhere so i've tried 
all sorts to try and escape this behaviour, including:

file.clearerr()
file.seek()
file.flush()

Nothing seems to work. What am i missing? It must be something 
simple. Any ideas?


More information about the Digitalmars-d-learn mailing list