How to read text files that have line feed and carriage return intermixed using X++?



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?
share|improve this question

2 Answers

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


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.