System_Write Append to New line in file

Mehr
2 Monate 4 Tage her #439 von Daniel De Renzy
Hi,

Thank you for your previous help on converting strings to bytes and using the System_Open set of functions to write strings to a file.

Unfortunately the code from the forum post that you sent through is only able to be used to open file once and write a line of data.

How can I continue to open the file and append new strings of data to a new line each time?

I have reviewed the help files for the System_Open and System_Write functions and am not quite sure how to achieve this.

Any help you can give would be greatly appreciated.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
2 Monate 3 Tage her #440 von Walter Zögernitz
Hello Daniel,


the code in this sample will check for an existing file first and will append the data. In case the file does not exist, it will create a new one:
PROGRAM RevolutionPiFilewriteProgram
	VAR CONSTANT
        /* Append new information to the end of the file. */
        Linux_O_APPEND : DINT := 1024;
        /* If the file does not exist, create it. If the O_CREAT option is used, then you must include the third parameter (mode). */
        Linux_O_CREAT  : DINT := 64;
        /* Combined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail. */
        Linux_O_EXCL   : DINT := 128;
        /* Open the file so that it is read only. */
        Linux_O_RDONLY : DINT := 0;
        /* Open the file so that it can be read from and written to. */
        Linux_O_RDWR   : DINT := 2;
        /* Initially clear all data from the file. */
        Linux_O_TRUNC  : DINT := 512;
        /* Open the file so that it is write only. */
        Linux_O_WRONLY : DINT := 1;
	END_VAR
	
	VAR
		INIT : BOOL:=TRUE;
		writeCounter : DINT := 0;
		fileDescriptor : DINT := 0;
		data : ARRAY[0..63] of BYTE;
        flagsWriteAppend : DINT := -1;
    	flagsCreateTruncWrite : DINT := -1;
		flags : DINT :=-1;
        mode : DINT := 0;
		my_string : STRING[64] := 'Hello World';
	END_VAR
		
	IF INIT THEN	
		/* write access, append to file content */
       	flagsWriteAppend := IOR(LINUX_O_WRONLY, LINUX_O_APPEND);
       	/* write access, create file and truncate file to length 0 */
       	flagsCreateTruncWrite := IOR(LINUX_O_WRONLY, LINUX_O_CREAT, LINUX_O_TRUNC);
       	/* read and write access for the user */
		
 	  	fileDescriptor := System_open(pathname := '/tmp/file.log', flags := flagsWriteAppend, ENO => ENO);
        IF NOT(ENO) THEN                            /* file does not exist yet */
        	fileDescriptor := System_open(pathname := '/tmp/file.log', flags := flagsCreateTruncWrite, mode := mode, ENO => ENO);
        END_IF;
	  	
		INIT:=FALSE;
	END_IF;
	
	/* Transfering string to BYTE array */
	  data := StrToByteArr(my_string);	  
    
      System_write(fd := fileDescriptor, data := data, count := LEN(my_string));     
    
      System_close(fd := fileDescriptor);
		
END_PROGRAM


kind regards
Walter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
2 Monate 2 Tage her #443 von Daniel De Renzy
Hi Walter,

Thanks for your response. This is greatly helped me in diagnosing the issue.

Firstly, I have noticed that the value you assign to o_append in the above code (1024) is different from that used (8) in the example provided in the Logicad user documentation ( help.logicals.com/lco3docu/latest/user-d...system_open-baustein ), or specified in the fcntl.h docs ( unix.superglobalmegacorp.com/Net2/newsrc/sys/fcntl.h.html ).

It seams that setting o_append to '8' does not append the file but over-writes the string previously written. In addition, setting o_append to '1024' as you have done appends the new string to the end of the previous string:
e.g 'Hello WorldHello WorldHello WorldHello WorldHello World'

I would still like to append the new string to a new line within the text document. As such my ideal output would be:
e.g 'Hello World
Hello World
Hello World
Hello World
Hello World'

I suspect this can be achieved by either setting o_append to a different value (however it is not clear to me which value this should be), or adding in some type of character to the string (e.g. '\n') but I am not quite sure how to do this. What is the best way to achieve this type of file output?

I know we are very close to my desired output, and any help would be greatly appreciated.

Regards,

Daniel

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
2 Monate 2 Tage her #444 von Walter Zögernitz
Hello Daniel,

Please send us your project (not your whole work space) as a ZIP file. We will look for solutions.

kind regards
Walter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
2 Monate 2 Tage her #445 von Daniel De Renzy
Hi Walter,

The project zip file that I have been using can be found at the end of the following forum post:
revolution.kunbus.de/forum/viewtopic.php...t=736&start=10#p3429

Thanks and regards,

Daniel

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
2 Monate 2 Tage her #446 von Walter Zögernitz
Hello Daniel,

for "jumping into" a new line you need to use character combination "$n".

kind regards
Walter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

logi.cals Österreich

Adresse

Europaplatz 7/1,
3100 St. Pölten

logi.cals Deutschland

Adresse

Postfach 1306,
40738 Langenfeld

Benutzer

Registrieren
© 1987 - 2019 logi.cals GmbH