Datei - Standard (BEISPIEL)

 

(* Beispiel wie CAA_FILE.library – Standard-Dateifunktionsblöcke zu verwenden sind *)

PROGRAM FILE.STANDARD_PRG

VAR
       xFileStdInit:       BOOL:=FALSE;
       uiFileStdState:     UINT:=0;
        sFileName:          CAA.FILENAME:= 'TestFile.txt';
       hFile:              CAA.HANDLE;
       sFileTestString:    STRING:='Hello caa library user';
       sFileString:        STRING(100):='';
       szFileSize1:        CAA.SIZE := 0;
       szFileSize2:        CAA.SIZE := 0;
       filop:              FILE.Open;
       filwr:              FILE.Write;
       filrd:              FILE.Read;
       filcl:              FILE.Close;
       filsp:              FILE.SetPos;
END_VAR


IF NOT xFileStdInit THEN
   filop(xExecute:=FALSE);
   filcl(xExecute:=FALSE);
   filwr(xExecute:=FALSE);
   filrd(xExecute:=FALSE);
   xFileStdInit:=TRUE;
   uiFileStdState:=0;
ELSE
   CASE uiFileStdState OF
       0:(* create a new file *)
           filop.sFileName:=sFileName;
           filop.eFileMode:=FILE.MODE.MRDWR;
           filop.xExclusive:=TRUE;
           filop( xExecute:=TRUE);
           IF filop.xDone THEN
               hFile:=filop.hFile;
               uiFileStdState:=1;
           END_IF
           IF filop.xError THEN
               (* error handling*)
               ;
           END_IF

       1:(* write text in the file *)
           filwr.hFile:=hFile;
           filwr.pBuffer:=ADR(sFileTestString);
           szFileSize1:=SIZEOF(sFileTestString);
           filwr.szSize:=szFileSize1;
           filwr.udiTimeOut:=100000;    (* 100ms Timeout *)
           filwr( xExecute:=TRUE);
           IF filwr.xDone THEN
               uiFileStdState:=2;
           END_IF
           IF filwr.xError THEN
               (* error handling*)
               ;
           END_IF

       2:(* set the internal positon file pointer *)
           filsp.hFile:= hFile;
           filsp.udiPos:=0;
           filsp( xExecute:=TRUE);
           IF filsp.xDone THEN
               uiFileStdState:=3;
           END_IF

           IF filsp.xError THEN
               (* error handling*)
               ;
           END_IF

       3:(* read file - TestFile.txt*)
           filrd.hFile:=hFile;
           filrd.udiTimeOut:=100000;    (* 100ms Timeout *)
           filrd.pBuffer:=ADR(sFileString);
           filrd.szBuffer:=SIZEOF (sFileString);
           filrd( xExecute:=TRUE);
           IF filrd.xDone THEN
               szFileSize2:=filrd.szSize;
               IF szFileSize2 = szFileSize1 THEN
                   uiFileStdState:=4;
               ELSE
                   (* error handling*)
                   ;
               END_IF
           END_IF
           IF filrd.xError THEN
               (* error handling*)
               ;
           END_IF

       4:(* close file  - TestFile.txt *)
           filcl.hFile:=hFile;
           filcl( xExecute:=TRUE);
           IF filcl.xDone THEN
               uiFileStdState:=4;
           END_IF
           IF filcl.xError THEN
               (* error handling*)
               ;
           END_IF

       5:(* end of example *)
           ;
   END_CASE
END_IF