且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示文本文件中的最后n行

更新时间:2023-08-26 18:35:34

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中写道:

我被要求编写一个简单的程序来显示给定文本文件中的最后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.