只读最后一行 - [英] Read only last line-

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

问题描述

您好 -


我正在尝试编写一个代码片段,在每行上打开一个带有

整数的文本文件。我想读一下

文件中的最后一个整数。我目前正在使用:

file = fopen(" f.txt"," r +");

fseek(file,-2,SEEK_END);

fscanf(文件,%d,& c);

如果整数只是一个字符,这个工作正常。当我将
变成更大的数字(例如502)时,它只读取2.是否有

我可以做的任何事情来读取最后一行作为实体而不是循环

然后整个文件?提前致谢 -


-Ryan

Hello-

I am trying to write a snippet which will open a text file with an
integer on each line. I would like to read the last integer in the
file. I am currently using:
file = fopen("f.txt", "r+");
fseek(file, -2, SEEK_END);
fscanf(file, "%d", &c);
this works fine if the integer is only a single character. When I get
into larger numbers though (e.g. 502) it only reads in the 2. Is there
anything I can do to read the last line as an entity instead of looping
through then entire file? Thanks in advance-

-Ryan

推荐答案

Ry ***** @ gmail.com 写道:
我正在尝试编写一个片段,它将打开一个文本文件每行一个
整数。我想读一下
文件中的最后一个整数。我目前正在使用:
file = fopen(" f.txt"," r +");
fseek(file,-2,SEEK_END);
fscanf(文件, "%d",& c);
如果整数只是一个字符,这个工作正常。当我得到更大的数字(例如502)时,它只读入2.是否有任何我可以做的事情来读取最后一行作为一个实体而不是循环
然后整个文件?提前致谢 -
I am trying to write a snippet which will open a text file with an
integer on each line. I would like to read the last integer in the
file. I am currently using:
file = fopen("f.txt", "r+");
fseek(file, -2, SEEK_END);
fscanf(file, "%d", &c);
this works fine if the integer is only a single character. When I get
into larger numbers though (e.g. 502) it only reads in the 2. Is there
anything I can do to read the last line as an entity instead of looping
through then entire file? Thanks in advance-




不,没有。文本文件是一系列字符,而不是

序列的行;没有预先知道多长时间就没有办法直接跳到最后一行

的开头。


至于标准关注,fseek(文件,-2,SEEK_END)

调用未定义的行为。标准说:


对于文本流,偏移量应为零,或者偏移量应为早先成功调用ftell返回的
a值

函数在与同一文件关联的流上,并且从那里
应该是SEEK_SET。


ftell()的结果文本流包含未指定的

信息,仅在使用whence == SEEK_SET调用fseek()时可用。


实际上,如果你可以假设最后一个行短于,比如说
,80个字符,你可以*可能*躲过一个

fseek(文件,-80,SEEK_END),然后阅读所有内容到文件的末尾,然后抓住最后一行。但那仍然有点冒险;例如,如果系统将行尾编码为CR-LF

对,则fseek()可能会使您在CR和LF之间着陆。还有

总是存在你猜错的风险,最后一行真的是

90个字符长。


唯一可移植的方法是读取整个文件。你可以通过折断性能的可移植性来做得更好。


< OT>

Unix尾巴 - 1 QUOT;命令这样做。 GNU版本的源代码

of tail命令包含在coreutils包中。

实现无疑是非便携式的,比你需要的更复杂,但你可能会从中得到一些想法。

< / OT>


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



No, there isn''t. A text file is a sequence of characters, not a
sequence of lines; there''s no way to jump directly to the beginning of
the last line without knowing in advance how long it is.

As far as the standard is concerned, fseek(file, -2, SEEK_END)
invokes undefined behavior. The standard says:

For a text stream, either offset shall be zero, or offset shall be
a value returned by an earlier successful call to the ftell
function on a stream associated with the same file and whence
shall be SEEK_SET.

The result of ftell() on a text stream contains unspecified
information, usable only in a call to fseek() with whence==SEEK_SET.

Realistically, if you can assume that the last line is shorter than,
say, 80 characters, you can *probably* get away with doing an
fseek(file, -80, SEEK_END), then reading everything up to the end of
the file and grabbing just the last line from that. But that''s still
a bit risky; for example, if the system encodes end-of-line as a CR-LF
pair, the fseek() could land you between a CR and an LF. And there''s
always the risk that you''ve guessed wrong, and the last line is really
90 characters long.

The only portable approach is to read the entire file. You can likely
do better by trading off portability for performance.

<OT>
The Unix "tail -1" command does this. Source code for the GNU version
of the tail command is included in the coreutils package. The
implementation is undoubtedly non-portable and more complex than you
need, but you might get some ideas from it.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Ry ***** @ gmail。 com 写道:
你好 -

我正在尝试编写一个代码片段,它将在每行打开一个带有
整数的文本文件。我想读一下
文件中的最后一个整数。我目前正在使用:
file = fopen(" f.txt"," r +");
fseek(file,-2,SEEK_END);
fscanf(文件, "%d",& c);
如果整数只是一个字符,这个工作正常。当我得到更大的数字(例如502)时,它只读入2.是否有任何我可以做的事情来读取最后一行作为一个实体而不是循环
然后整个文件?提前致谢 -
Hello-

I am trying to write a snippet which will open a text file with an
integer on each line. I would like to read the last integer in the
file. I am currently using:
file = fopen("f.txt", "r+");
fseek(file, -2, SEEK_END);
fscanf(file, "%d", &c);
this works fine if the integer is only a single character. When I get
into larger numbers though (e.g. 502) it only reads in the 2. Is there
anything I can do to read the last line as an entity instead of looping
through then entire file? Thanks in advance-




不能用完美的便携式C,部分

,因为你使用的方式fseek()不可移植:


对于文本流,偏移量应为零,

或偏移量应为早期返回的值

成功调用ftell函数[...]和

将是SEEK_SET。 (7.19.9.2/4)


另一个问题是每一行(第一行除外)

在''\ n''之后立即开始结束前面的

行;前面的''\ n''标志着后续的

字符作为线路启动器。你无法判断一个文本文件中的任意位置是否是或者不是一行的开始

而没有阅读前面的字符看看

是否是换行符。另一个问题是,磁盘上的文件可能会用其他一些

的方式标记行结尾,其编码只能转换为''\ nn ''

通过向前阅读它。


从头到尾阅读整个文件只是完美的

便携式方法。记得你最近阅读的行最近读了,直到你成功阅读了另一行,并且当你到达文件结尾时,b / b
记住的行是最后一行

一个。 (您可能还希望使用ftell()来记住每行开头的

位置,这样你就可以再次将fseek()返回到

。)除非文件非常大,这是一个完全合理的方法。


如果文件很大,你愿意尝试

不可携带的东西(也就是说,不能保证
工作),你可以尝试寻找一个大约十个或十几个的地方
$ b在结束前使用$ b行''的值,并在文件尾部使用read-and-remember

方法。这可能 - *可能* - 工作;

只有你可以判断风险是否值得奖励。


-

Eric Sosman
es*****@acm-dot-org.inva


Ry*****@gmail.com 写道:
你好 -

我正在尝试编写一个代码片段,它会在每行打开一个带有
整数的文本文件。我想读一下
文件中的最后一个整数。我目前正在使用:
file = fopen(" f.txt"," r +");
fseek(file,-2,SEEK_END);
fscanf(文件, "%d",& c);
如果整数只是一个字符,这个工作正常。当我得到更大的数字(例如502)时,它只读入2.是否有任何我可以做的事情来读取最后一行作为一个实体而不是循环
然后整个文件?提前致谢 -
Hello-

I am trying to write a snippet which will open a text file with an
integer on each line. I would like to read the last integer in the
file. I am currently using:
file = fopen("f.txt", "r+");
fseek(file, -2, SEEK_END);
fscanf(file, "%d", &c);
this works fine if the integer is only a single character. When I get
into larger numbers though (e.g. 502) it only reads in the 2. Is there
anything I can do to read the last line as an entity instead of looping
through then entire file? Thanks in advance-




使用fseek建立你的号码,一次向后读一个角色

。别忘了检查文件操作是否失败以及

整数溢出。

-

Flash Gordon

生活在有趣的时代。

网站 - http ://home.flash-gordon.me.uk/

comp.lang.c发布guidlines和介绍 -
http://clc-wiki.net/wiki/Intro_to_clc


这篇关于只读最后一行 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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