什么是算在ASCII文件中的新行最简单的方法? [英] What is the easiest way to count the newlines in an ASCII file?

查看:180
本文介绍了什么是算在ASCII文件中的新行最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是得到一个ASCII文件的行的最快方法?

Which is the fastest way to get the lines of an ASCII file?

推荐答案

通常你使用C中读取文件与fgets 。您还可以使用 scanf函数(%[^ \\ n]),但不少人看完code就容易发现,混乱和国外。

Normally you read files in C using fgets. You can also use scanf("%[^\n]"), but quite a few people reading the code are likely to find that confusing and foreign.

编辑:在另一方面,如果你真的只是想计算行中, scanf函数办法略加修改的版本可以工作得很好:

on the other hand, if you really do just want to count lines, a slightly modified version of the scanf approach can work quite nicely:

while (EOF != (scanf("%*[^\n]"), scanf("%*c"))) 
    ++lines;

这样做的好处是,与*在每次转换, scanf的读取和输入匹配,但不执行任何其结果。这意味着我们不必浪费在一个大的缓冲存储器来保存我们不关心(并且仍然需要得到一条线,比这更大的机会行的内容,所以我们的错误计数结束除非我们到了连的更多的工作,以找出是否我们看到以新行结束输入)。

The advantage of this is that with the '*' in each conversion, scanf reads and matches the input, but does nothing with the result. That means we don't have to waste memory on a large buffer to hold the content of a line that we don't care about (and still take a chance of getting a line that's even larger than that, so our count ends up wrong unless we got to even more work to figure out whether the input we read ended with a newline).

不幸的是,我们要分手了 scanf函数成两片这样的。 scanf函数停止扫描时转换失败,如果输入包含一个空行(两个连续的换行),我们预计第一次转换失败。即使失败了,但是,我们希望第二次转换发生,读取下一个换行,并移动到下一行。因此,我们试图在第一次转换到吃的行的内容,然后执行%C 转换读取行(我们真正关心的部分)。我们将继续这样做既直到第二次调用 scanf函数收益 EOF (这通常是在文件的结尾,尽管它也可以发生在像读取错误)的情况。

Unfortunately, we do have to break up the scanf into two pieces like this. scanf stops scanning when a conversion fails, and if the input contains a blank line (two consecutive newlines) we expect the first conversion to fail. Even if that fails, however, we want the second conversion to happen, to read the next newline and move on to the next line. Therefore, we attempt the first conversion to "eat" the content of the line, and then do the %c conversion to read the newline (the part we really care about). We continue doing both until the second call to scanf returns EOF (which will normally be at the end of the file, though it can also happen in case of something like a read error).

EDIT2:当然,有一个(至少可以说)另一种可能性更简单,更容易理解:

Of course, there is another possibility that's (at least arguably) simpler and easier to understand:

int ch;

while (EOF != (ch=getchar()))
    if (ch=='\n')
        ++lines;

此的唯一部分,有些人觉得有悖常理的是, CH 必须的被定义为 INT ,而不是字符为code才能正常工作。

The only part of this that some people find counterintuitive is that ch must be defined as an int, not a char for the code to work correctly.

这篇关于什么是算在ASCII文件中的新行最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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