Modify

Opened 9 years ago

Closed 8 years ago

#3279 closed Bug (No Bug)

FileReadToArray() documentation missing important detail

Reported by: rumbata@… Owned by:
Milestone: Component: Documentation
Version: 3.3.14.2 Severity: None
Keywords: Cc:

Description

When using FileReadToArray() with a file handle, the file position may need to be set to the beginning of the file (depending the mode used when opening the file). Otherwise, the function may produce unexpected results. While this behaviour does make sense, it is not quite obvious and needs to be clearly documented or changed.

; Incorrect result
$filehandle = FileOpen("C:\temp\sample file.txt", $FO_APPEND+$FO_CREATEPATH) ; The sample file is not empty
FileReadToArray($filehandle)
MsgBox(0, 'Unexpected Error', '@error is ' & @error & ", but this is not expected as the file is not empty!")
; Presuming no other errors happened, @error will be set to 2 as if the file was empty

; Correct result
$filehandle = FileOpen("C:\temp\sample file.txt", $FO_APPEND+$FO_CREATEPATH) ; The sample file is not empty
FileSetPos($filehandle,0, 0)
FileReadToArray($filehandle)
MsgBox(0, 'Expected Result', '@error is ' & @error & " and this is expected because the file is not empty.")
; Presuming no other errors happened, @error will be set to 0 because the file is not empty

Attachments (0)

Change History (2)

comment:1 by jchd, 9 years ago

I disagree: after explicitely opening a file for append, it's plain that the handle's file pointer is set to EOF upon successful return of the function. Reading whatever from there is obviously going to fail and raise an error.
This is how low-level fileread/filewrite functions work in all languages I know of.
No low-level File* function has the right to change the file pointer before reading/writing! Doing so would make them completely useless in almost all situations.

That said I confess that the description in the documentation could have said "reading past EOF" or "EOF unexpectedly reached" like the other FileRead* functions do (with @error set to -1). Yet still, any volontary attempt to read anything from EOF is much more a programming error than a misleading documentation.

comment:2 by mLipok, 8 years ago

Resolution: No Bug
Status: newclosed

15 months with no other comment, the more there are no counterarguments against closure.
It was to be considered closed.

Modify Ticket

Action
as closed The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.