如何读取以二进制形式传输的文本文件

发布于 2024-09-28 12:32:53 字数 738 浏览 3 评论 0原文

我的代码将文件从 ftp(使用文本传输模式)复制到本地磁盘,然后尝试处理它们。 所有文件仅包含文本,值使用换行符分隔。有时,文件会使用二进制传输模式移动到此 ftp,看起来这会弄乱行尾。 使用十六进制编辑器,我根据用于将文件发送到 ftp 的传输模式来比较行尾: 使用文本模式:文件结尾为 0D 0A 使用二进制模式:文件结尾为 0D 0D 0A

是否可以修改我的代码,以便它可以在这两种情况下读取文件? 来自工作的代码说明了我的问题并显示了我如何读取文件: (这里我使用相同的文件,包含14行数据)

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)));
}

文件从Unix系统到Windows系统。 不确定,但问题可能是:“我应该使用哪个 inFieldDelimiter 值来读取 Unix 和 Windows 行尾?”

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?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-10-05 12:32:53

使用 inRecordDelimiter:

inFile.inRecordDelimiter('\n');

而不是:

inFile.inFieldDelimiter('\n');

最后一个字段上可能仍然有一个悬空的 CR,您可能希望删除它:

strRem(conpeek(con, conlen(con)), '\r')

另请参阅:http://en.wikipedia.org/wiki/Line_endings

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文