Home > Language Reference > File Functions > GetReadPos

GetReadPos

The GetReadPos function returns the value of the read pointer for the specified file.

Usage

Used to read a record in the middle of a file instead of starting the read at the beginning of the file.

Syntax

var=GetReadPos(<filename>)

Parameters

<filename>, string, name of the file.

Return value

var, integer. It returns the read pointer in bytes from the beginning of <filename> and so gives the position for the next read. The value of this pointer is 0 when the file is read for the first time, then WinTask increments it automatically. If the function succeeds, the return value is greater than 0. If the function fails, the return value is a zero value (see File Functions Error Codes). 

See also

SetReadPos
Read
Eof

Examples

var = GetReadPos("c:\wttest\my_file.txt")

'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