hexagon logo

FILE/EXISTS strange behaviour

Hello guys, I had encountered strange FILE/EXISTS command behaviour. I want to load some data from text file, if this file exists. Quite simple:

SUBROUTINE/DATUMOVKA_LOADER,
                PARTNAME =  : JMÉNO PROGRAMU,
                PRODDATE =  : DATUMOVKA DÍLU,
                COILBATCH =  : ŠARŽE COILU,
                COILNO =  : ČÍSLO COILU,
                MSG = 0 : VÝSTUPNÍ ZPRÁVA V PŘÍPADĚ NENALEZENÍ SOUBORU S DATUMOVKOU,
                 =
            ASSIGN/DATUMOVKA_FILE="X:\\Datumovky\\"+PARTNAME+".txt"
FILEOK     =FILE/EXISTS,DATUMOVKA_FILE
            IF/FILEOK==0
              ASSIGN/MSG="File does not exist! " + DATUMOVKA_FILE
              GOTO/HANDLER_DATUMOVKA_LOADER
            END_IF/
FPTR       =FILE/OPEN,DATUMOVKA_FILE,READ
            ASSIGN/ENDF=0
            ASSIGN/PRODDATE=-1
            ASSIGN/COILBATCH=-1
            ASSIGN/COILNO=-1
ENDF       =FILE/READLINE,FPTR,{PRODDATE}+";"+{COILBATCH}+";"+{COILNO}
            FILE/CLOSE,FPTR,KEEP
            ASSIGN/HODNOTY=ARRAY(PRODDATE,COILBATCH,COILNO)
            ASSIGN/TESTHODNOT=MIN(HODNOTY)
            IF/TESTHODNOT<0
              ASSIGN/MSG="Improper values loaded, check file " + DATUMOVKA_FILE
            END_IF/
HANDLER_DATUMOVKA_LOADER=LABEL/
            ENDSUB/
DATELOAD   =GROUP/SHOWALLPARAMS=YES
              ASSIGN/PD=0
              ASSIGN/CB=0
              ASSIGN/CN=0
              ASSIGN/MG=0
              ASSIGN/ADD="atest"
CS1          =CALLSUB/DATUMOVKA_LOADER,X:\_CMM_Programy\_Subroutines\subroutines_002.PRG:ADD,PD,CB,CN,MG,,
              IF/MG<>0
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,OVC=NO,
                MG
              END_IF/
              ELSE/
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,OVC=NO,
                PD
                CB
                CN
              END_ELSE/
            ENDGROUP/ID=DATELOAD​


The problem is that FILEOK returns "1" even if the file does not exist and than instead of quitting the routine the program tries to read the data from file which lead to error, because the file is not there obviously Slight smile After hours of trying everything the code started to work after this modification:
FILEOK     =FILE/EXISTS,DATUMOVKA_FILE
            ASSIGN/FILEOK=FILEOK
            IF/FILEOK==0​
.
.
.

Everything else is exactly the same. You know, I can live with that, even if it does not make sense to me. But does anyone have an idea HOW IS THIS F...ING POSSIBLE? AngrySlight smile
Parents
  • ​ I love that piece of advice and sadly I'm not surprised by it at all! Wink

    ​ I see you're working with subroutines and the file's location is created from one of the parameters. In my experience, this can be tricky if the string doesn't evaluate to something useful in every possible scenario----including at the very time of writing the code for your subroutine. That's why I always make sure to have suitable default values for my subroutines even if they will never be used later on.
    Another piece of information that might be helpful is that I sometimes had to explicitly cast my parameters to what I wanted them to be. I suspect that your apparently useless line of assigning your FILEOK variable to itself might actually have triggered a type change underneath the hood, but of course that's just a guess.
Reply
  • ​ I love that piece of advice and sadly I'm not surprised by it at all! Wink

    ​ I see you're working with subroutines and the file's location is created from one of the parameters. In my experience, this can be tricky if the string doesn't evaluate to something useful in every possible scenario----including at the very time of writing the code for your subroutine. That's why I always make sure to have suitable default values for my subroutines even if they will never be used later on.
    Another piece of information that might be helpful is that I sometimes had to explicitly cast my parameters to what I wanted them to be. I suspect that your apparently useless line of assigning your FILEOK variable to itself might actually have triggered a type change underneath the hood, but of course that's just a guess.
Children
No Data