Forum > FPC development

FileSeek appears to be documented different than implementation

(1/1)

lagprogramming:
Acording to RTL reference guide
76.15.111 FileSeek
Synopsis: Set the current file position on a file handle.
Errors: On error, -1 is returned.
Description: FileSeek sets the file pointer on position Offset, starting from Origin....If successful, the function returns the new file position, relative to the beginning of the file.

The following code can be found in the development(trunc) of rtl/unix/sysutils.pp:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function FileSeek (Handle,FOffset,Origin : Longint) : Longint; Var  I : Int64; begin  I:=FileSeek(Handle,int64(FOffset),Origin);  if I>High(Longint) then     Raise EInOutError.CreateFmt(SErrPosToBigForLongint,[I]);  result:=I;end;
As can be seen, on error the function may return -1 but it may raise exceptions, too. Should the documentation be updated?
Should I understand that the appropriate programming practice for using FileSeek is including it in a "try if FileSeek<>-1 then begin...end; except/finnaly end;" block?
Wouldn't be better if the function would return a negative value different than -1 instead of that exception, for example error code -2 or something?

MarkMLl:
The documentation https://www.freepascal.org/docs-html/current/rtl/sysutils/fileseek.html also says that there are two implementations of FileSeek(), one of which takes a LongInt and the other an Int64.

I suggest removing that Int64() cast and retesting.

MarkMLl

lagprogramming:
Let's assume that in a code that should work on different platforms FileSeek is called using zero and fsFromEnd.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FileSeek(FileHandle, 0, fsFromEnd);As an example, in a linux-x86_64 environment the assembly window shows a call to <SYSUTILS_$$_FILESEEK$LONGINT$LONGINT$LONGINT$$LONGINT>
Even though the above code is legitimate, probably depending on the target platform, if FileHandle points to a file larger than High(Longint) then FileSeek(FileHandle, 0, fsFromEnd) might raise an exception.
"Write once, compile anywhere!" :-\

AlexTP:

--- Quote ---As can be seen, on error the function may return -1 but it may raise exceptions, too. Should the documentation be updated?
--- End quote ---
I don't see the error in the FileSeek. Why doc should be changed? What is wrong with it? -1 result will be handled ok.

Here one 'overload' of FileSeek calls another 'overload' with Int64.

Navigation

[0] Message Index

Go to full version