Writing to and reading from a file using the C/AL code
Even though the XMLport takes care of the file integration requirements, sometimes we may want to perform this activity by using the C/AL code. This recipe will demonstrate how to read or write from a file using the C/AL code.
How to do it...
Let's start by creating a new codeunit from Object Designer.
Add the following global variables:
Name
Type
Length
StremOut
OutStrem
FileOut
File
StremIn
InStrem
FileIn
File
TextLine
Text
250
Add the following code in the
OnRun
trigger:IF NOT FileOut.CREATE('D:\NAVFile.txt') THEN IF NOT FileOut.OPEN('D:\NAVFile.txt') THEN ERROR('Unable to write to file!'); FileOut.CREATEOUTSTREAM(StreamOut); StreamOut.WRITETEXT('Line 1'); StreamOut.WRITETEXT(); StreamOut.WRITETEXT('Line 2'); StreamOut.WRITETEXT(); FileOut.CLOSE; IF NOT FileIn.OPEN('D:\NAVFile.txt') THEN ERROR('Unable to read file!'); FileIn.CREATEINSTREAM(StreamIn); WHILE NOT StreamIn.EOS DO BEGIN StreamIn.READTEXT(TextLine...