seek
Description:
Sets the file position indicator for the file stream stream to the value pointed to by offset.
This function can be used to set the indicator beyond the actual end of the file, however, negative position values are not accepted.
For text streams, the only valid values of offset are 0 (applicable to any origin) and a value returned by an earlier call to ftell (only applicable to SET).
Example: Change the current position:
public static int main (string[] args) {
// open file:
FileStream stream = FileStream.open ("test.txt", "w+");
assert (stream != null);
// write data:
// file content: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for (char c = 'A'; c <= 'Z' ; c++) {
stream.putc (c);
}
stream.flush ();
// get the 1. char:
// file content: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// file ptr: ^
int tmp = stream.seek (0, FileSeek.SET);
assert (tmp >= 0);
int c = stream.getc ();
assert (c >= 0);
print ("First char: %c\n", c);
// Get the next but one char:
// file content: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// file ptr: ^
stream.ungetc (c); // revert .getc
tmp = stream.seek (2, FileSeek.CUR);
assert (c >= 0);
c = stream.getc ();
assert (c >= 0);
print ("Third char: %c\n", c);
// Get the last char:
// file content: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// file ptr: ^
stream.ungetc (c); // revert .getc
tmp = stream.seek (-1, FileSeek.END);
assert (c >= 0);
c = stream.getc ();
assert (c >= 0);
print ("Last char: %c\n", c);
return 0;
}
valac --pkg glib-2.0 GLib.FileStream.seek.vala
Parameters:
offset |
number of characters to shift the position relative to origin |
whence |
position to which offset is added. |
Returns:
0 upon success, nonzero value otherwise. Associated GLib.FileStream.EOF flag is cleared for the stream and the effect of any ungetc is undone. |