File – internal pointer (EXAMPLE)

(* example of how to use the CAA.FILE-library - file position function blocks *)

PROGRAM FILE.POINT_PRG

VAR

                xFilePosInit:                            BOOL:=FALSE;

                uiFilePosState:                        UINT:=0;

                udiActualPosition:                   UDINT:=0;

                udiActualEoFPosition:             UDINT:=0;

                sFileName:                              CAA.FILENAME:= 'TestFile.txt';

                hFile:                                       CAA.HANDLE;

                filop:                                       FILE.Open;

                filcl:                                         FILE.Close;

                filgp:                                       FILE.GetPos;

                filsp:                                       FILE.SetPos;

                fileof:                                      FILE.EOF;

END_VAR

 

IF NOT xFilePosInit THEN

                filop(xExecute:=FALSE);

                filcl(xExecute:=FALSE);

                filgp(xExecute:=FALSE);

                filsp(xExecute:=FALSE);

                fileof(xExecute:=FALSE);

                xFilePosInit:=TRUE;

                uiFilePosState:=0;

ELSE

                CASE uiFilePosState OF

                0:(* open file *)

                               filop.sFileName:= sFileName;

                               filop.eFileMode:=FILE.MRDWR;

                               filop.xExclusive:=TRUE;

                               filop( xExecute:=TRUE);

                               IF filop.xDone THEN

                                               hFile:=filop.hFile;

                                               uiFilePosState:=1;

                               END_IF

                               IF filop.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                1: (* get actual internal positon file pointer *)

                               filgp.hFile:= hFile;

                               filgp( xExecute:=TRUE);

                               IF filgp.xDone THEN

                                               udiActualPosition:=filgp.udiPos;

                                               uiFilePosState:=2;

                               END_IF

                               IF filgp.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                2:(* query - end of file is reached  *)

                               fileof.hFile:= hFile;

                               fileof( xExecute:=TRUE);

                               IF fileof.xDone THEN

                                               IF fileof.xEOF THEN

                                                               udiActualEoFPosition:=udiActualPosition;

                                               END_IF

                                               uiFilePosState:=3;

                               END_IF

                               IF filgp.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

 

 

                3: (* set the internal positon file pointer *)

                               filsp.hFile:= hFile;

                               filsp.udiPos:=udiActualEoFPosition - 5;

                               IF filsp.udiPos < 0 THEN

                                               filsp.udiPos:=0;

                               END_IF

                               filsp( xExecute:=TRUE);

                               IF filsp.xDone THEN

                                               uiFilePosState:=4;

                               END_IF

                               IF filsp.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                4:(* close file *)

                               filcl.hFile:=hFile;

                               filcl( xExecute:=TRUE);

                               IF filcl.xDone THEN

                                               uiFilePosState:=5;

                               END_IF

                               IF filcl.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                5: (* end of example*)

                               ;

                END_CASE

END_IF