Home > Language Reference > File Functions > Read

Read

The Read function reads data from the specified file.

Usage

Used to 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

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

Parameters

<filename>, string, file to read. If the path is not specified, the file is searched in the current working folder.

<buffer>, string, variable only. It contains 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 Read, you can move the pointer by using SetReadPos function. 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 file 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.

<separator>, string. The Read is performed until the next occurrence of <separator>. <buffer> contains the bytes read between the initial position and <separator> without including it. The read pointer is set to the next character following <separator>. If <separator> is not found, <buffer> contains the bytes between the read pointer and the end of file and EOF(<filename>) is set to 1.

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

Return value

ret, optional numeric return value. If the function succeeds, the return value is 0. If the function fails, the return value is a nonzero value (see File Functions Error Codes). 

See also

Write
Eof
GetReadPos
SetReadPos
ReadExcel
ReadIni$
ReadReg

Examples

Read(file$,var$)

Read("c:\wttest\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"



'This script writes some data into a txt file and then Read function is used to read its records.
FileName$="c:\wttest\data.txt"
'Write 5 records in filename$
write(filename$,"record 1",CRLF)
write(filename$,"record 2",CRLF)
write(filename$,"record 3",CRLF)
write(filename$,"record 4",CRLF)
write(filename$,"record 5",CRLF)

 

'Read the file just created
repeat
    Read(FileName$,result$,CRLF)
    display$=display$+"\n\"+result$
until eof(FileName$)=1
'Display the results 
msgbox(display$,0,"data.txt 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:\wttest\file1.txt"
FileName2$="c:\wttest\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