问题
My code copies files from ftp (using text transfer mode) to local disk and then trys to process them. All files contain only text and values are seperated using new line. Sometimes files are moved to this ftp using binary transfer mode and looks like this will mess up line-ends. Using hex editor, I compared line ends depending the transfer mode used to send files to ftp: using text mode: file endings are 0D 0A using binary mode: file endings are 0D 0D 0A
Is it possible to modify my code so it could read files in both cases? Code from job that illustrates my problem and shows how i'm reading file: (here i use same file, that contains 14 rows of data)
int i;
container con;
container files = ["c:\\temp\\axa_keio\\ascii.txt", "c:\\temp\\axa_keio\\binary.txt"];
boolean purchLineFirstRow;
IO inFile;
;
for(i=1; i<=conlen(files); i++)
{
inFile = new AsciiIO(conpeek(files,i), "R");
inFile.inFieldDelimiter('\n');
con = inFile.read();
info(int2str(conlen(con)));
}
Files come from Unix system to Windows sytem. Not sure but maybe the question could be: "Which inFieldDelimiter values should i use to read both Unix and Windows line ends?"
回答1:
Use inRecordDelimiter
:
inFile.inRecordDelimiter('\n');
instead of:
inFile.inFieldDelimiter('\n');
There may still be a dangling CR on the last field, you may wish remove this:
strRem(conpeek(con, conlen(con)), '\r')
See also: http://en.wikipedia.org/wiki/Line_endings
来源:https://stackoverflow.com/questions/4008711/how-to-read-text-files-transfered-as-binary