以相反的顺序读取文件(bootom-top) [英] reading a file in reverse order (bootom-top)

查看:66
本文介绍了以相反的顺序读取文件(bootom-top)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个问题。


我尝试以相反的顺序打印ascii文件(从下到上)。这是逻辑。


1.转到文件fseek()的底部。移回一个字符以避免EOF。

2.从这里读取一个字符,打印它,将文件指针(FILE *)移回2步(使用fseek(fp,-2,SEEK_CUR) ))阅读前一个字符。


如果文件只有一行(即没有新行字符),这似乎没问题。如果它遇到一个新的行字符并进入一个无限循环,引用一系列换行符,则上述逻辑失败。


为了解决这个问题我检查了字符读取,如果是新行字符,我将文件指针移动3步(fseek(fp,-3,SEEK_CUR))并且现在这个逻辑工作得很好。


任何人都可以解释一下为什么要对这个新行字符进行特殊考虑。


非常感谢提前。


谢谢

Praveen

解决方案

sahukar praveen写道:< blockquote class =post_quotes>
第1.1部分类型:纯文本(文本/纯文本)
编码:quoted-printable




请不要在clc中发布html或附件

-

Chuck F(cb********@yahoo.com)(cb *** *****@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net> ;使用worldnet地址!


" sahukar praveen" < SA ************ @ yahoo.co.in>在留言新闻中写道:< 3f ****** @ usenet01.boi.hp.com> ...

你好,

我有一个问题。

我尝试以相反的顺序打印ascii文件(从下到上)。这是
逻辑。

1.转到文件fseek()的底部。将一个角色移回
避免EOF。
2.从这里读取一个角色,打印出来,将文件指针(FILE *)移回2步(使用fseek(fp, -2,SEEK CUR))阅读上一个
字符。

如果文件只有一行(即没有新行
字符),这似乎没问题。上面的逻辑如果遇到一个新的行字符就会失败,并进入一个无限循环,引发一系列新行字符。

为了解决这个问题,我检查了字符读取,如果是是新行的
字符,我将文件指针移动3步(fseek(fp,-3,SEEK CUR))
现在逻辑工作正常。

可以任何人都请解释一下为什么要特别考虑一下新线角色。

非常感谢提前。

谢谢
Praveen -




某些平台代表一个带有回车符和换行符的换行符,而不是一个换行符,而不是一个换行符。为什么你需要备份三个字符而不是两个字符。


而不是一次只读一个字符,你可能想要

将一大块字符抓取到缓冲区中,然后从该缓冲区打印。

一方面,它减少了对fseek()的调用次数(可能是

是一个相当昂贵的操作),你可以跳过你不想打印的

字符。


2003年11月25日星期二16:38:48 + 0530,在comp.lang.c,sahukar praveen

< sa ********** **@yahoo.co.in>写道:

你好,

我有一个问题。

我尝试以相反的顺序打印ascii文件(底部 - 顶部)。




一个更快的方法,如果你有内存将是malloc一块

内存你认为足够大的内存整个文件,fread()文件

进入它,转到最后一个字节并向后走过块。

PLatform特定的扩展名将帮助你找到你的内存量

需要。

-

Mark McIntyre

CLC FAQ< http://www.eskimo。 com /~scs / C-faq / top.html>

CLC自述文件:< http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


Hello,

I have a question.

I try to print a ascii file in reverse order( bottom-top). Here is the logic.

1. Go to the botton of the file fseek(). move one character back to avoid the EOF.
2. From here read a character, print it, move the file pointer (FILE*) to 2 steps back (using fseek(fp, -2, SEEK_CUR)) to read the previous character.

This seems to be ok if the file has a single line (i.e. no new line character). The above logic fails if it encounters a new line character and gets into an infinite loop priting a series of new-line character.

To fix this I checked for the character read and if it is new-line character, I move the file pointer by 3 steps (fseek(fp, -3, SEEK_CUR)) and now the logic works fine.

Can anyone please explain me why a this special consideration for a new-line character.

Many Thanks in advance.

Thanks
Praveen

解决方案

sahukar praveen wrote:


Part 1.1 Type: Plain Text (text/plain)
Encoding: quoted-printable



Please do not post html or attachments in c.l.c.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


"sahukar praveen" <sa************@yahoo.co.in> wrote in message news:<3f******@usenet01.boi.hp.com>...

Hello,

I have a question.

I try to print a ascii file in reverse order( bottom-top). Here is the
logic.

1. Go to the botton of the file fseek(). move one character back to
avoid the EOF.
2. From here read a character, print it, move the file pointer (FILE*)
to 2 steps back (using fseek(fp, -2, SEEK CUR)) to read the previous
character.

This seems to be ok if the file has a single line (i.e. no new line
character). The above logic fails if it encounters a new line character
and gets into an infinite loop priting a series of new-line character.

To fix this I checked for the character read and if it is new-line
character, I move the file pointer by 3 steps (fseek(fp, -3, SEEK CUR))
and now the logic works fine.

Can anyone please explain me why a this special consideration for a
new-line character.

Many Thanks in advance.

Thanks
Praveen
--



Some platforms represent a newline with a carriage return and line
feed pair, rather than a single newline character, which is why you
need to back up three characters instead of two.

Instead of reading a single character at a time, you might want to
grab a chunk of characters into a buffer, then print from that buffer.
For one thing, it reduces the number of calls to fseek() (which may
be a fairly expensive operation), and you can just skip over the
characters you don''t want to print.


On Tue, 25 Nov 2003 16:38:48 +0530, in comp.lang.c , "sahukar praveen"
<sa************@yahoo.co.in> wrote:

Hello,

I have a question.

I try to print a ascii file in reverse order( bottom-top).



A quicker way if you have enugh memory would be to malloc a block of
memory you think is large enough for the entire file, fread() the file
into it, go to the last byte and walk backwards through the block.
PLatform specific extensions would help you find how much memory you
needed.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


这篇关于以相反的顺序读取文件(bootom-top)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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