Re: How to wait for file closing?
- Posted by mattlewis (admin) Dec 10, 2012
- 1801 views
I need to open and read (but not write) this file by my Euphoria program. How to be convinced of what the file is already closed? Otherwise it can be read not completely. I have no ideas, except adding keyword to end of file. For example, "END". But it isn't so effective because it is necessary to read all file to be convinced that it is full. Maybe, it is superfluous because Euphoria won't open file during writing?
You can lock the file exclusively when writing, and then attempt to get a shared lock when reading. Here is some euphoria code that demonstrates this:
-- write.ex include std/io.e integer fn = open("foo.txt", "w") lock_file( fn, LOCK_EXCLUSIVE ) puts(fn, "start!\n") system_exec("eui read.ex", -2) puts(fn, "end!\n") close(fn) system_exec("eui read.ex", -2) ------------------------------------------ -- read.ex include std/io.e include std/console.e puts(1, "READING:\n") integer fn = open( "foo.txt", "r") if fn != 1 then if lock_file( fn, LOCK_SHARED ) then display( read_lines( fn ) ) end if close(fn) end if puts(1, "DONE\n")
In C, you could lock a file like this on Linux:
FILE* f = fopen("foo.txt", "w"); int result = flock(fileno(f)), LOCK_EX);
The process is similar in Windows, I think.
Alternatively, you could communicate via sockets to tell your euphoria program that it should read the file.
Matt