Question

I am trying to read a text file using Dynamics AX. However, the following code replaces any spaces in the lines with commas:

// Open file for read access
myFile = new TextIo(fileName , 'R');

myFile.inFieldDelimiter('\n');

fileRecord = myFile.read();
while (fileRecord)
{
    line = con2str(fileRecord);
    info(line);
…

I have tried various combinations of the above code, including specifying a blank '' field delimiter, but with the same behaviour.

The following code works, but seems like there should be a better way to do this:

// Open file for read access
myFile = new TextIo(fileName , 'R');

myFile.inRecordDelimiter('\n');
myFile.inFieldDelimiter('_stringnotinfile_');

fileRecord = myFile.read();
while (fileRecord)
{
    line = con2str(fileRecord);
    info(line);

The format of the file is field format. For example:

DATAFIELD1    DATAFIELD2  DATAFIELD3
DATAFIELD1                DATAFIELD3
DATAFIELD1    DATAFIELD2  DATAFIELD3

So what I end up with unless I use the workaround above is something like:

line=DATAFIELD1,DATAFIELD2,DATAFIELD3

The underlying problem here is that I have mixed input formats. Some of the files just have line feeds {LF} and others have {CR}{LF}. Using my workaround above seems to work for both. Is there a way to deal with both, or to strip \r from the file?

Was it helpful?

Solution

Con2Str:

Con2Str will retrieve a list of values from a container and by default uses comma (,) to separate the values.

client server public static str Con2Str(container c, [str sep])

If no value for the sep parameter is specified, the comma character will be inserted between elements in the returned string.

Possible options:

  1. If you would like the space to be the default separator, you can pass space as the second parameter to the method Con2Str.

  2. One other option is that you can also loop through the container fileRecord to fetch the individual elements.

Code snippet 1:

Below code snippet loads the file contents into textbuffer and replace the carriage returns (\r) with new line (\n) character. The condition if (strlen(line) > 1) will help to skip empty strings due to the possible occurrence of consecutive newline characters.

TextBuffer  textBuffer;
str         textString;
str         clearText;
int         newLinePos;
str         line;
str         field1;
str         field2;
str         field3;
counter     row;
;

textBuffer  = new TextBuffer();
textBuffer.fromFile(@"C:\temp\Input.txt");
textString  = textBuffer.getText();
clearText   = strreplace(textString, '\r', '\n');

row = 0;
while (strlen(clearText) > 0 )
{
    row++;
    newLinePos  = strfind(clearText, '\n', 1, strlen(clearText));
    line        = (newLinePos == 0 ? clearText : substr(clearText, 1, newLinePos));

    if (strlen(line) > 1)
    {
        field1  = substr(line, 1, 14);
        field2  = substr(line, 15, 12);
        field3  = substr(line, 27, 10);

        info('Row ' + int2str(row) + ', Column 1: ' + field1);
        info('Row ' + int2str(row) + ', Column 2: ' + field2);
        info('Row ' + int2str(row) + ', Column 3: ' + field3);
    }

    clearText =  (newLinePos == 0 ? '' : substr(clearText, newLinePos + 1, strlen(clearText) - newLinePos));
}

Code snippet 2:

You could use File macro instead of hard coding the values \r\n and R that denotes the read mode.

TextIo      inputFile;
container   fileRecord;
str         line;
str         field1;
str         field2;
str         field3;
counter     row;
;

inputFile = new TextIo(@"c:\temp\Input.txt", 'R');

inputFile.inFieldDelimiter("\r\n");

row = 0;
while (inputFile.status() == IO_Status::Ok)
{
    row++;
    fileRecord  = inputFile.read();
    line        = con2str(fileRecord);

    if (line != '')
    {
        field1  = substr(line, 1, 14);
        field2  = substr(line, 15, 12);
        field3  = substr(line, 27, 10);

        info('Row ' + int2str(row) + ', Column 1: ' + field1);
        info('Row ' + int2str(row) + ', Column 2: ' + field2);
        info('Row ' + int2str(row) + ', Column 3: ' + field3);
    }
}

Dynamics AX job output

OTHER TIPS

Never tried to use the default RecordDelimiter as FieldDelimiter and not setting another RecordDelimiter explicitly. Normally rows (Records) are delimited by \n and fields are delimited by comma, tab, semicolon or some other symbol. You might also be hitting some weird behaviour when TextIO is assuming correct UTF-format. You didn't supply an example of some rows from you datafile, so guessing is hard.

Read more about TextIO here: http://msdn.microsoft.com/en-us/library/aa603840.aspx

EDIT: With the additional example of file content, it seems to me the file is a fixed width file, where each column has its own fixed width. I would rather recommend using subStr if that is the case. Read about substr here: http://msdn.microsoft.com/en-us/library/aa677836.aspx

use StrAlpha to restrict blank values after you convert Con2Str

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top