Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Error in example for FileReadLine function


JamesMc Aug 18, 2016 04:31 PM

In the CRBasic Program Reference .chm file the page for function FileReadLine states that:

"The FileReadLine function returns the number of bytes successfully read or -1 if the end of the file is reached."

However, the associated programming example is incorrect.  The loop in the following example code would never terminate (in certain situations possibly):

      'FileReadLine example
      FileHandle2=FileOpen ("USR:read.txt","r",0)
      Do
         ReadFile2=FileReadLine(FileHandle2,ReadDest2,10)
         CallTable (ReadDat1)
      Loop Until ReadFile2=0
      FileClose (FileHandle2)

 Should it not Loop Until ReadFile2 = -1?


JDavis Aug 19, 2016 03:09 PM

The current example in CRBasic help does show reading the file until -1 is returned. I prefer to do a read until the result is <= 1. That would catch either 0 or -1. The data table calls in the example program were mixed up. I am submitting that change.

Public FileHandle1, ReadFile1, ReadDest1 As String
Public FileHandle2, ReadFile2, ReadDest2 As String

DataTable (ReadDat1,True,-1)
Sample (1,ReadDest1,String)
EndTable

DataTable (ReadDat2,True,-1)
Sample (1,ReadDest2,String)
EndTable

BeginProg
SetStatus ("USRDriveSize",16384)

Scan (1,Sec,3,1)

'FileRead example
FileHandle1=FileOpen ("USR:read.txt","r",0)
ReadFile1= FileRead(FileHandle1,ReadDest1,4)
FileClose (FileHandle1)

CallTable (ReadDat1)

'FileReadLine example
FileHandle2=FileOpen ("USR:read.txt","r",0)
Do
ReadFile2=FileReadLine(FileHandle2,ReadDest2,10)
CallTable (ReadDat2)
Loop Until ReadFile2<=1

FileClose (FileHandle2)
NextScan
EndProg

Log in or register to post/reply in the forum.