He leído esto en one of Perl manuals, pero se traduce fácilmente en la norma C, que, a su vez, se puede traducir a istream
s.
seek FILEHANDLE,POSITION,WHENCE
Sets FILEHANDLE's position, just like the "fseek" call of
"stdio".
<...>
A WHENCE of 1 ("SEEK_CUR") is useful for not moving the file
position:
seek(TEST,0,1);
This is also useful for applications emulating "tail -f". Once
you hit EOF on your read, and then sleep for a while, you might
have to stick in a seek() to reset things. The "seek" doesn't
change the current position, but it does clear the end-of-file
condition on the handle, so that the next "<FILE>" makes Perl
try again to read something. We hope.
Por lo que yo recuerdo, fseek
se llama iostream::seekg
. Por lo tanto, básicamente debe hacer lo mismo: busque hasta el final del archivo, luego duerma y vuelva a intentar con el indicador ios_base::cur
para actualizar el final del archivo y leer algunos datos más.
En lugar de sleep
ing, puede utilizar inotify, como se sugiere en the other answer, para dormir (bloquear al leer desde un archivo emulado, en realidad) exactamente hasta que el archivo se actualice/cierre. Pero eso es específico de Linux, y no es C++ estándar.
Es posible que tenga dificultades para hacer esto en C++ puro . Deberá usar alguna API específica de la plataforma. (Para empezar, no creo que pueda abrir un archivo en C++ de forma no exclusiva.) – sbi
@sbi No creo que el estándar C++ tenga algo que decir sobre exclusividad. –
¿Hay alguna razón por la que no puedas usar tail -f? –