Read

File management function.

The Read function reads data from the specified file.

Usage

You read data from an external souce, a text file. For example, in a loop, you read each record stored in a text file, send the data to another application and the loop ends when the end of file is reached.

Syntax

Read(<filename>,<buffer>)
Read(<filename>,<buffer>,<n>)
Read(<filename>,<buffer>,<sep$>)
Read(<filename>,<buffer>,CRLF)
or
ret=Read(<filename>,<buffer>)
ret=Read(<filename>,<buffer>,<n>)
ret=Read(<filename>,<buffer>,<sep$>)
ret=Read(<filename>,<buffer>,CRLF)

Parameters

<filename>, string, name of the file to read. It is a string which can be a constant or a variable. Long names and UNC names are accepted, such as \\Server\c\my_directory\my_file.txt. If the path is not specified, the file is searched in the current directory.

<buffer>, string ; it must be a variable. The variable is filled with the result of the Read function.

The Read is done sequentially starting at the read pointer associated with the file. After a Read, the pointer is automatically moved to the next byte not already read. Between two Reads, you can move the pointer by using the SetReadPos(<filename>) statement. If a Read encounters the end of file, then EOF(<filename>) is set to 1.

<n>, integer, number of bytes to read. If the length of <filename> is less than n, the entire files is copied into the variable <buffer> and EOF(<filename>) is set to 1, if the length is more than n, <buffer> contains the first n bytes and the read pointer is set for reading the following n bytes.

<sep$>, string (variable or constant). The Read is performed until the next occurrence of <sep$>. <buffer> contains the bytes read between the initial position until sep$ (sep$ is not placed in <buffer>). The read pointer is set to the next character following sep$.

 
If sep$ is not found during a Read, <buffer> contains the bytes between the read pointer and the end of file. EOF(<filename>) is set to 1.

CRLF : carriage-return-line-feed keyword (chr$(13)+chr$(10)). The file is read line by line.

Return value

Ret, numeric return code. When the read is successful, the function returns 0, otherwise use this return code for Error Handling.

Examples

 

Read(file$,var$)

Read("c:\sample.txt", var$)

Read(file$,var$,80) ' puts the next 80 characters in var$

Read(file$,var$,n) ' puts the next n characters in var$

Read(file$,var$,CRLF) ' puts the next line of file$ in var$

Read(file$,var$,"mark") ' puts the characters between the pointer and "mark" in var$, but var$ does not include "mark"

Example code

This script reads the autoexec.bat file.

 

msgbox(OsVersion$())

FileName$="c:\autoexec.bat"

 

repeat

Read(FileName$,result$,CRLF)

display$=display$+"\n\"+result$

until eof(FileName$)=1

 

msgbox(display$,0,"Autoexec.bat File")

This script reads two files simultaneously. It explains the use of the two commands GetReadPos and SetReadPos. It creates, writes and reads files

 

FileName1$="c:\file1.txt"

FileName2$="c:\file2.txt"

 

create(FileName1$)

create(FileName2$)

 

'write into file1.txt "File 1 Line 1")

Write(FileName1$,"File 1 Line 1",CRLF)

 

'write into file1.txt "File 1 Line 2"

delim$="Line 2"+CRLF

Write(FileName1$,"File 1 ",delim$)

 

'write into file1.txt "File 1 Line 3"

delim$=CRLF

Write(FileName1$,"File 1 ")

Write(FileName1$,"Line 3",Delim$)

 

'write 4 different lines into file2.txt

awrite$="File 2 Line 1"+CRLF+"File 2 Line 2"+CRLF+"File 2 Line 3"+CRLF+"File 2 Line 4"

 

write(Filename2$,awrite$)

 

 

'loop reading the files File1.txt and File2.txt

 

display1$="Sequential reading of the file File1.txt : "+"\n\"

display2$="Sequential reading of the file File2.txt : "+"\n\"

 

 

i=0

 

repeat

result$=""

 

if eof(FileName1$)<>1 then 'if we are not at the end of the file

read(FileName1$,Result$,CRLF)

endif

 

'after having read the last record, eof is positioned to 1

 

display1$=display1$+"\n\"+result$

 

result$=""

 

if eof(FileName2$)<>1 then 'if we are not at the end of the file

read(FileName2$,Result$,CRLF)

endif

 

'after having read the last record, eof is positioned to 1

 

display2$=display2$+"\n\"+result$

 

pause 1

MsgFrame(display1$,1,5,10,8)

MsgFrame(display2$,2,230,10,8)

 

i=i+1

if i=1 then 'if we have just read the first record

FilePos1=GetReadPos(FileName1$)

'we remember the position of the pointer of the file 1

endif

 

if i=3 then 'if we have just read the fourth record

FilePos2=GetReadPos(FileName2$)

'we remember the position of the pointer of the file 2

endif

 

until eof(FileName1$)=1 and eof(FileName2$)=1

 

SetReadPos(FileName1$,0)

'this is how to reposition to the beginning of file 1

 

SetReadPos(FileName2$,0)

'this is how to reposition to the beginning of file 2

 

'management of the pointers of files

MsgFrame("** Reading of the files by using SetReadPos **",9,40,160,8)

 

FileRead1$="Reading record 2 in file 1"+"\n\\n\"

FileRead2$="Reading record 4 in file 2"+"\n\\n\"

 

'we go back to the second record (previously saved)

SetReadPos(FileName1$,FilePos1)

read(FileName1$,result$,CRLF) 'to read second line

FileRead1$=FileRead1$+"\n\"+result$

MsgFrame(FileRead1$,3,5,220,8)

 

pause 4

 

SetReadPos(FileName2$,FilePos2)

read(FileName2$,result$,CRLF) 'to read second line

FileRead2$=FileRead2$+"\n\"+result$

MsgFrame(FileRead2$,3,5,220,8)

 

pause 4