FileReadLine Function

Action

Returns the next line of a file on the host system.

Syntax

bDidRead = FileReadLine(hFile, sLine)
Variable Description
bDidRead Whether a line was read. BOOLEAN.
hFile The handle of the file to read a line from. HFILE.
sLine A variable to hold the line. out STRING.

Notes

  • FileReadLine reads the next line of a file and returns the contents of the line in sLine. Typically, you open a file with FileOpen, and then read it line by line with FileReadLine until FALSE is returned.

  • You can get a handle to a file (an HFILE) by calling the FileOpen function with the FM_READ filemode data type. An exception is raised if an invalid file handle is specified.

  • FileReadLine returns TRUE if a line was read, or FALSE if the end of the file was encountered.

  • FileReadLine modifies the sLine variable. Any previous value in sLine is discarded.

  • FileReadLine considers the end-of-line character to be a carriage return (CR), even if it is not combined with a linefeed (LF) character. In other words, both CR and CR-LF are considered line terminators.

  • 4Test recognizes that it has reached the end of a file (EOF) by no longer reading that file – in other words, by receiving a null string. It does not read any special character at the EOF.

  • For Silk Test Classic, EOF is indicated by a FALSE bDidRead return and a null string is indicated by a TRUE bDidRead return and a null string.

  • This function can handle line lengths up to 4K characters.

  • This function is not designed for remote access.

Examples

[ ] HFILE hFile
[ ] STRING sLine
[-] testcase FileReadExample()
	[ ] hFile = FileOpen("mydata.txt", FM_READ)
	[ ] while(FileReadLine(hFile, sLine))
	[ ]   // statements to process this line
	[ ] FileClose(hFile)
You can also read from a file opened in file mode FM_UPDATE, in order to begin overwriting the file in the middle instead of at the beginning. For example:
[ ] STRING s
[ ] INTEGER i
[ ] HFILE hFile = FileOpen("{GetProgramDir()}\Sample.txt", FM_UPDATE)
[-] for i = 1 to 2
	[ ] FileReadLine(hFile, s)
[ ] FileWriteLine(hFile, "*New one*")
[ ] FileWriteLine(hFile, "*This is new line two*")
[ ] FileClose (hFile)
[ ] 
[ ] // Before:
[ ] // Line 1
[ ] // Line 2
[ ] // Line 3
[ ] // Line 4
[ ] // Line 5
[ ] 
[ ] // After:
[ ] // Line 1
[ ] // Line 2
[ ] // *New one*
[ ] // *This is new line two*