Modify

Opened 4 years ago

Closed 3 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 Changed 4 years ago by jchd

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 Changed 3 years ago by mLipok

  • Resolution set to No Bug
  • Status changed from new to closed

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

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.