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?
Con2Str:
Con2Str will retrieve a list of values from a container and by default uses
comma (,)to separate the values.If no value for the
sepparameter is specified, the comma character will be inserted between elements in the returned string.Possible options:
If you would like the space to be the default separator, you can pass space as the second parameter to the method
Con2Str.One other option is that you can also loop through the container
fileRecordto 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 conditionif (strlen(line) > 1)will help to skip empty strings due to the possible occurrence of consecutive newline characters.Code snippet 2:
You could use File macro instead of hard coding the values
\r\nandRthat denotes the read mode.