fseek()
Sets the pointer of the file f from the position whence to the position defined by the offset.
Synopsis
int fseek(file f, int offset, int whence);
Parameter
Parameter | Description |
---|---|
f | File whose pointer will be set. |
offset | Number of bytes the position of the pointer moves against whence. |
whence. |
Position defined as a CTRL constant; SEEK_SET start of the files (starts with 0) SEEK_CUR current position SEEK_END ende of file |
Return value
In the event of errors, the function returns -1 or EOF otherwise, 0.
Error
If incorrect data types are used or if a file is not open.
Description
The fseek sets the file position indicator for the stream pointed to by f. The new position measured in bytes is obtained by adding offset bytes file to the position of whence. If whence is set to SEEK:SET, SEEK_CUR or SEEK _END, the offset is relative to the start of the file, the current position or the end of a file, respectivly.
Example
The following example opens a file, obtains the position of the pointer and sets the position to different values.
main()
{
int tell, seek, offset, whence;
file f;
string dat, mod;
f=fopen("C:/TEMP/Abb.txt", "r+"); // Open file
// tell at the begin to 0
tell=ftell(f);
DebugN(tell); // 0
// set pointer to 30
seek=fseek(f, 30, SEEK_SET);
tell=ftell(f);
DebugN(tell);// 30
// set pointer to 30 from the current position
seek=fseek(f, 30, SEEK_CUR);
tell=ftell(f);
DebugN(tell); // 60
// set pointer to the end + 60
seek=fseek(f, 60, SEEK_END);
tell=ftell(f);
DebugN(tell); // 8224
// set pointer to the end
seek=fseek(f, 0, SEEK_END);
tell=ftell(f);
DebugN(tell); // 8164
// set pointer again to end + 60
seek=fseek(f, 60, SEEK_CUR);
tell=ftell(f);
DebugN(tell); // 8224
}
Assignment
File functions
Availability
CTRL