HOWTO write text files with long lines

ShenpenShenpen Member Posts: 386
edited 2005-12-13 in NAV Tips & Tricks
What if you have to write a banking interface for Navision, where a line in the text file can easily more than 1024 chars long?

1) You cannot write one variable for a line, as a text var can only be 1024 chars long.

2) You cannot write in binary mode, as Navision will insert zero bytes after strings.

3) You could try to concatenate two or more text variables when writing, but even if it works, that's an ugly hack and it is hard to build a general "interface framework" on top of it.

The right solution is OUTSTREAM.

variables:

CR: char
LF: char
CRLF : text(2)
ThisFile: file
Stream: OutStream
CR:=13;
LF:=10;
CRLF:=format(CR)+format(LF);

ThisFIle.TEXTMODE(FALSE);
ThisFile.CREATE('c:\dammit.txt');
ThisFile.CREATEOUTSTREAM(Stream);
Stream.WRITETEXT('Ha!');
Stream.WRITETEXT('No zeroes, no spaces, no newlines, no problem!');
Stream.WRITETEXT(CRLF);
Stream.WRITETEXT('See?');
ThisFile.CLOSE;

Do It Yourself is they key. Standard code might work - your code surely works.

Comments

  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    You just might want to 'optimize' your code by skipping variables CR and LF. Instead initialize variable CRLF like this:
    CRLF[1] := 13;
    CRLF[2] := 10;
    
    :D
  • krikikriki Member, Moderator Posts: 9,112
    A file in Binary mode works,but with some tricks.
    Fil.WRITE(COPYSTR(txtMyString,1,STRLEN(txtMyString)); // the COPYSTR is needed, because otherwise the full length of txtMyString is written.

    fil.SEEK(fil.POS - 1); // this repositions on the zero-byte and will be overwritten by the next WRITE.
    Also the CRLF you made, I used.

    So, now also the poor guys that are still working with older Navision-versions can write longer strings.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ShenpenShenpen Member Posts: 386
    And what happens with the zero byte of the last write in a file?

    Do It Yourself is they key. Standard code might work - your code surely works.
  • krikikriki Member, Moderator Posts: 9,112
    Shenpen wrote:
    And what happens with the zero byte of the last write in a file?
    If I'm not mistaken, the last fil.SEEK(fil.POS - 1); puts the file-pointer on the last zero byte and then you close the file, deleting the last zero byte.

    I did something like this when no streams existed and it worked.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.