CSV File Writing Overwrite error RPi

Anonymous
2017-05-06
2018-03-14
  • Anonymous - 2017-05-06

    Originally created by: Primitive Source

    Hello, I am a bit of a newbie to the whole programming world but have learnt a fair bit over the passed few months. My latest stumbling block is writing to a csv file created in my programme on the pi. I can create the file successfully, and write to it each time a :BOOL is triggered TRUE. However each time I trigger this switch, I overwrite the existing data. I am using .CAA library.

    Where am i going wrong in the code below, this is taken from the example in the help section and amended slightly for my string/ triggers. I first of all create a file with one case/one :BOOL name RUN. Then the file is opened again in the Append mode by :BOOL named FiWr but still seems to overwrite each time is triggered rather than adding to the end of the file

    Var
    RUN: BOOL;
    FiWr: BOOL;
    FINSTR:= CONCAT(SEMFINSTR,'$R$N');
    csvwrite:= UDINT:=0;
    END_VAR

    xFileStdInit:= RUN;

    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:( close file - TestFile.txt )
    filcl.hFile:=hFile;
    filcl( xExecute:=TRUE);
    IF filcl.xDone THEN
    uiFileStdState:=2;
    END_IF
    IF filcl.xError THEN
    ( error handling)
    ;
    END_IF

        2:(* end of example *)
            ;
    END_CASE
    

    END_IF

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

        1:(* write text in the file *)
            filwr.hFile:=hFile;
            filwr.pBuffer:=ADR(FINSTR);
            szFileSize1:=SIZEOF(FINSTR);
            filwr.szSize:=szFileSize1;
            filwr.udiTimeOut:=100000;    (* 100ms Timeout *)
            filwr( xExecute:=TRUE);
            IF filwr.xDone THEN
                csvwrite:=2;
            END_IF
            IF filwr.xError THEN
                (* error handling*)
                ;
            END_IF
    
        2:(* close file  - TestFile.txt *)
            filcl.hFile:=hFile;
            filcl( xExecute:=TRUE);
            IF filcl.xDone THEN
                csvwrite:=4;
            END_IF
            IF filcl.xError THEN
                (* error handling*)
                ;
            END_IF
    
        3:(* end of example *)
            ;
    END_CASE
    

    END_IF

    I hope you can help

     
  • SpeedyG - 2018-03-14

    Hi Primitive Source,

    it seems we have kind of the same problem here, but you are one step ahead of me. So I also want to write & read a csv-file in a certain directory. As my code is mostly similar and you wrote

    Zitat:
    I can create the file successfully, and write to it each time a :BOOL is triggered TRUE.

    I want to ask you: Where is your file stored? And where in your code do you influence on where it's going to be stored?
    Did you solve your problem? I know it's been one year already, but hopefully you care to help me a bit

    Kind regards,
    SpeedyG

     
  • PJE - 2018-03-14

    Are you opening the file in write W or append A mode?

    I've not looked at your library, but the one I used allowed me to open the file with append to add data - just be careful the file doesn't get too large.

    Sent from my iPhone using Tapatalk Pro

     

Log in to post a comment.