C
Chris Torek
[on fseek()ing to offset -1 from SEEK_END]
Obviously you have not used VMS.
VMS has dozens of file formats, and using the -1 trick works on
some of them, but not all of them. So it would depend on the
file format of the file you opened.
The answer (which by now should be obvious) to the first part of
the quesetion (in the subject line) is "it is not fully portable".
As for whether it is "smart", that one is trickier.
In my ancient TeX-DVI-file-handling library, which had a rather
different but related problem to solve, I had a machine-dependent
function I called "make seekable", so that you could string DVI-file
commands together with pipes. Some readers here in in comp.lang.c
may be aware that Unix-like systems (including Linux) cause seek
(including fseek()) operations on pipes to fail. Even if you
fopen() your file, it is possible that the name refers to a pipe
(e.g., a "named pipe", or perhaps simply /dev/stdin), so that the
seek will fail.
The fully-portable, but ugly, solution is simply to copy the entire
file, adding a newline at the end if and only if the original
version did not have one. This is obviously going to be slower
than a machine-specific function that can use the seek-to-end trick.
Whether it is "significantly" slower depends on many other things.
Yes, but in the instant case I don't think it is possible for it
to have worked thousands of times on a particular "implementation"
and then to quit working,
Obviously you have not used VMS.
and as a matter of fact, if it works ONCE I can't see why it
would fail to work forever...
VMS has dozens of file formats, and using the -1 trick works on
some of them, but not all of them. So it would depend on the
file format of the file you opened.
The answer (which by now should be obvious) to the first part of
the quesetion (in the subject line) is "it is not fully portable".
As for whether it is "smart", that one is trickier.
In my ancient TeX-DVI-file-handling library, which had a rather
different but related problem to solve, I had a machine-dependent
function I called "make seekable", so that you could string DVI-file
commands together with pipes. Some readers here in in comp.lang.c
may be aware that Unix-like systems (including Linux) cause seek
(including fseek()) operations on pipes to fail. Even if you
fopen() your file, it is possible that the name refers to a pipe
(e.g., a "named pipe", or perhaps simply /dev/stdin), so that the
seek will fail.
The fully-portable, but ugly, solution is simply to copy the entire
file, adding a newline at the end if and only if the original
version did not have one. This is obviously going to be slower
than a machine-specific function that can use the seek-to-end trick.
Whether it is "significantly" slower depends on many other things.