Writing UNIX style text files

I’ve written many mods for AX that required output of data to a text file of some form (the most common being CSV), but recently I had to ensure that the file being written was compliant with a UNIX reader.

The default output for files created from within AX are UTF-16LE with the standard Windows CR+LF new-line characters. A UNIX system that expects ANSI formatting with just a single LF character as the new-line sequence will not be able to read these files due to additional characters prepended to the beginning of the file for the code page designator and the additional carriage return character at the end of each line.

The TextIo object in AX provides us with the necessary settings to allow this specific type of output with very little additional coding.

static void UNIX_TextFile(Args _args)
    str outputPath = @"C:\Temp\ANSI.txt";
    TextIo outputFile = new TextIo(outputPath,"W", 0);//ANSI code page

    outputFile.outRecordDelimiter(num2char(10)); //UNIX line-feed character
    outputFile = null;

In the above example, using 0 as the code page parameter when creating the TextIo object specifies that the output file should use ANSI formatting (additional code page values can be found on this MSDN page).

In order to use the UNIX style new-line, we call the outRecordDelimiter method with the numeric value of the UNIX line-feed character 10. This causes all “records” written to the file to be followed with a single line-feed character instead of the standard Windows carriage-return/line-feed sequence.

There are many Blog entries around the web that go into much greater detail of how the File IO classes available in AX actually work, but I was unable to find any that specifically outlined a solution for writing UNIX styled text files from within AX so I decided to provide one of my own. If you know of any others feel free to provide links.


This entry was posted in AX 2012 and tagged , , , , . Bookmark the permalink.