Applies To:
  • CitectSCADA 3.xx, 4.xx, 5.xx

Summary:
I have defined an ASCII device in the Device Definitions form, and I have put -1 in the No. Files field which should mean that my text file keeps getting appended to each time I write to this device. I have written some Cicode which I call from a button. This code opens the device, writes to it, and closes the device. But each time I push the button, the text file it has generated only contains data from the previous code execution. It has not been appended to. What am I doing wrong.

My test code looks like this:

FUNCTION AddRecord()
INT hDev;
hDev= DevOpen("TextFile",0);
DevWriteLn(hDev,"Hello");
DevWriteLn(hDev,"Goodbye");
DevClose(hDev);
END


Solution:
When you call DevOpen(), the device pointer is at the top of the file. Hence, the subsequent DevWriteLn()'s overwrite the existing text. You can add the following lines to your code, so the pointer is placed at the end of the file. Your code should look like:

FUNCTION AddRecord()
INT hDev;
INT NoRec;
hDev= DevOpen("TextFile",0);
NoRec=DevSize(hDev);
DevSeek(hDev,NoRec);
DevWriteLn(hDev,"Hello");
DevWriteLn(hDev,"Goodbye");
DevClose(hDev);
END

 

Keywords:
 

Attachments