| offsetBytes |
(Stream stream [, double offset]) |
yoix.io |
| |
Gets or sets the position of the file pointer in
stream.
If
offset
is absent or less than zero, then the current position of the file pointer is
returned.
If
offset
is greater than or equal to zero, then the file pointer is moved to the
specified offset and the new file pointer position is returned.
The offset is
measured in bytes from the beginning of the underlying stream,
which must be a file opened for reading and writing for this built-in to
succeed.
If an error occurred or the
stream
does not support offset operations, a
-1
is returned.
Output streams are flushed as a result of this operation and
input streams have their read buffers reset as needed.
Note that when transitioning from reading to writing,
offsetBytes
should be called to get the current offset position so as to reset the
buffers as needed.
There are instances when such buffer resets are not needed during a
read / write transition, but calling
offsetBytes
in that case does no harm.
| |
| Example: |
The following script opens a temporary file for reading and writing.
Assuming that offset operations are supported, which they are in this
instance, it writes several lines into the file (including a "TYPO"),
returns to the beginning of the
file, reads in the first line and writes it to standard output and on
standard error it indicates where it ended reading in the file and
also the saved offset value.
The script now corrects the typo and prints the revised offset information
on standard error..
It then goes back to the saved offset position in the file, continues writing,
returns to the beginning again and writes everything back to standard output.
Finally, on standard error it once again
indicates where it ended reading in the file and
the latest saved offset position.
Some clean-up finishes the script.
import yoix.io.*;
import yoix.stdio.*;
import yoix.system.*;
File f = {
String name = tmpnam(null);
int mode = READ|WRITE;
int open = true;
};
if (offsetSupported(f)) {
f.nextbuf = "Midway in our life's journey, I went astray\n\
TYPO the straight road and woke to find myself\n\
alone in a dark wood.";
double pos = offsetBytes(f);
offsetBytes(f, 0);
fprintf(stdout, "First line: \"%s\"\n\n", f.nextline);
fprintf(stderr,
"Current file pointer: %d\nSaved offset is at: %d\n\n",
offsetBytes(f), pos);
f.nextbuf = "from";
fprintf(stderr,
"Current file pointer: %d\nSaved offset is at: %d\n\n",
offsetBytes(f), pos);
offsetBytes(f, pos);
f.nextbuf = " How shall I say\n\n\
what wood that was! I never saw so drear,\n\
so rank, so arduous a wilderness!\n\
Its very memory gives a shape to fear.\n\n";
pos = offsetBytes(f);
offsetBytes(f, 0);
stdout.nextbuf = f.nextbuf;
fprintf(stderr,
"Current file pointer: %d\nSaved offset is at: %d\n",
offsetBytes(f), pos);
} else {
fprintf(stderr,
"Offset operations not supported on this stream.\n");
}
f.open = false;
unlink(f.fullname);
The output, combining both standard output and standard error, looks like:
First line: "Midway in our life's journey, I went astray"
Current file pointer: 44
Saved offset is at: 112
Current file pointer: 48
Saved offset is at: 112
Midway in our life's journey, I went astray
from the straight road and woke to find myself
alone in a dark wood. How shall I say
what wood that was! I never saw so drear,
so rank, so arduous a wilderness!
Its very memory gives a shape to fear.
Current file pointer: 248
Saved offset is at: 248
Note that if a call to
offsetBytes
had not been performed between the reading of the first line and the
correction of the typo, the correction would not have been properly
positioned.
The reason is that,
although the read appears to have ended in the correct place, the
buffered read actually put the file pointer at the end of the file.
On the other hand, at the end of the example, the read leaves the file
pointer at the end of the file, so no special positioning would be
needed if the next write intended to append to the file.
| | |
| Return: |
double
| | |
| See Also: |
offsetSupported,
mark,
reset
|
|
Yoix is a registered trademark of AT&T Inc.
|
|