如何显示文本文件中的最后n行 [英] How to display the last n lines from a text file

查看:68
本文介绍了如何显示文本文件中的最后n行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我被要求编写一个简单的程序来显示

给定文本文件中的最后n行。但我不知道C如何定义线。在文本中

文件。它是如何判断它是否是行的结束,是否有这样的事情

如EOF那样调用EOL?

-

已发布通过 http://dbforums.com

解决方案

2003年9月12日星期五00:23:19 -0400,nic977< me ********* @ dbforums.com>

写道:


我被要求编写一个简单的程序来显示
给定文本文件中的最后n行。但我不知道C如何定义线。在文本
文件中。它是如何判断它是否是行的结尾,是否有这样的事情如EOF那样调用EOL?




你以文本模式打开文件,例如:


FILE * f = fopen(" example.txt"," r");


在此模式下全部换行符转换为整数''\ n''。


尼克。


星期五, 2003年9月12日00:23:19 -0400,nic977< me ********* @ dbforums.com>

在comp.lang.c中写道:
< blockquote class =post_quotes>
我被要求编写一个简单的程序来显示给定文本文件中的最后n行。但我不知道C如何定义线。在文本
文件中。它是如何判断它是否是行的结束,是否有这样的事情如EOF那样调用EOL?




是的,它被命名为'' \ n''。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang的常见问题解答.c http://www.eskimo.com/~ scs / C-faq / top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq


nic977写道:

我被要求编写一个简单的程序来显示给出的最后n行
文本文件。但我不知道C如何定义线。在文本
文件中。它是如何判断它是否是行的结束,是否有这样的事情如EOF那样调用EOL?




否;你只需要寻找换行符。做你想做的最简单的方法

是查看文件中的每个字符,并计算换行数。



I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?
--
Posted via http://dbforums.com

解决方案

On Fri, 12 Sep 2003 00:23:19 -0400, nic977 <me*********@dbforums.com>
wrote:


I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?



You open the file in text mode, e.g:

FILE *f = fopen( "example.txt", "r" );

In this mode all newlines are translated to the integer ''\n''.

Nick.


On Fri, 12 Sep 2003 00:23:19 -0400, nic977 <me*********@dbforums.com>
wrote in comp.lang.c:


I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?



Yes, it is named ''\n''.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


nic977 wrote:

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?



No; you just look for newlines. The simplest way to do what you want
is to look at each character in the file, and count how many are newlines.


这篇关于如何显示文本文件中的最后n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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