如何在Octave中计算文本文件中的行数? [英] How to count the number of lines in a text file in Octave?

查看:142
本文介绍了如何在Octave中计算文本文件中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也不要说fskipl,因为它不起作用!!!

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

fskipl undefined.

推荐答案

您有fgetl吗?如果是这样,您可以执行以下小循环:

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是换行符的ASCII码.但这似乎不可靠,尤其是当您遇到使用回车而不是换行符的文件时.

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.

这篇关于如何在Octave中计算文本文件中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆