如何计算Octave中文本文​​件的行数?

发布于 2024-12-21 18:18:53 字数 75 浏览 2 评论 0原文

并且不要说 fskipl 因为它不起作用!

fskipl 未定义

And don't say fskipl because it doesn't work!!!

fskipl undefined.

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

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

发布评论

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

评论(1

梦里梦着梦中梦 2024-12-28 18:18:53

你有fgetl吗?如果是这样,你可以做这个小循环:

f = fopen('myfile.txt', 'rt');
ctr = 0;
ll = fgetl(f);
while (!isnumeric(ll)) %# fgetl returns -1 when it hits eof. But you can't do ll != -1 because blank lines make it barf
    ctr = ctr+1;
    ll = fgetl(f);
end
fclose(f);

否则,你可以做一些 hack,比如:

f = fopen('myfile.txt', 'rb');
ctr = 0;
[x, bytes] = fread(f, 8192); %# use an 8k intermediate buffer, change this value as desired
while (bytes > 0)
    ctr = ctr + sum(x == 10); %# 10 is '\n'
    [x, bytes] = fread(f, 8192);
end
fclose(f);

10 是换行符的 ASCII 代码。但这似乎不可靠,特别是当您遇到使用回车符而不是换行符的文件时。

Do you have fgetl? If so, you can do this little loop:

f = fopen('myfile.txt', 'rt');
ctr = 0;
ll = fgetl(f);
while (!isnumeric(ll)) %# fgetl returns -1 when it hits eof. But you can't do ll != -1 because blank lines make it barf
    ctr = ctr+1;
    ll = fgetl(f);
end
fclose(f);

Otherwise, you could do some hack like:

f = fopen('myfile.txt', 'rb');
ctr = 0;
[x, bytes] = fread(f, 8192); %# use an 8k intermediate buffer, change this value as desired
while (bytes > 0)
    ctr = ctr + sum(x == 10); %# 10 is '\n'
    [x, bytes] = fread(f, 8192);
end
fclose(f);

10 is the ASCII code for the newline character. But this seems unreliable, especially if you come across a file that uses carriage return instead of newline.

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