撤消与ungetc()的作用:"如何" fseek的做(),快退()和fsetpos()做呢?是缓冲每次重新填充? [英] Undoing the effects of ungetc() : "How" do fseek(),rewind() and fsetpos() do it?Is buffer refilled each time?

查看:205
本文介绍了撤消与ungetc()的作用:"如何" fseek的做(),快退()和fsetpos()做呢?是缓冲每次重新填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哼!我怎样将整个事情在一个清晰的问题!让我来试试:

Huh!!How shall I put the whole thing in a clear question!!Let me try:

据了解,现在使用的文件打开 fopen()函数被缓存到memory.We使用缓冲区的效率,并从文件ease.During读,内容文件首先被读取到缓冲器,我们从buffer.Similarly,在该文件的写入时,内容被写入到缓冲器,然后再对文件读

I know that the files opened using fopen() are buffered into memory.We use a buffer for efficiency and ease.During a read from the file, the contents of the file are first read to the buffer,and we read from that buffer.Similarly,in a write to the file, the contents are written to the buffer first ,and then to the file.

但什么 fseek的() fsetpos()退() 丢弃的previous的效果调用与ungetc()?你能告诉我如何这是做什么?我的意思是,给我们打开读取文件并将其复制到使用buffer.Now 与ungetc()我们已经改变了一些字符在buffer.Here就是我刚才不明白即使经过很大的努力:

But what with fseek(),fsetpos() and rewind()dropping the effect of the previous calls to ungetc()? Can you tell me how it is done?I mean,given we have opened a file for read and it is copied into the buffer.Now using ungetc() we've changed some characters in the buffer.Here is what I just fail to understand even after much effort:


  • 下面就是谈到的与ungetc() - A调用FSEEK,fsetpos或rewind投产将放弃任何字符previously放回它具有这种功能。的--How已经可以放入缓冲区的字符被丢弃?一种方法是被删除的,原来的人物被放在记忆,而每一个新的角色标识和原有的character.But取代似乎很inefficient.The另一种选择是在这两个的预期position.Which方法原始文件的副本加载到缓冲区中,然后将文件指针确实fseek的,fsetpos或rewind拿放弃使用字符放在与ungetc()

  • Here's what said about the ungetc() --"A call to fseek, fsetpos or rewind on stream will discard any characters previously put back into it with this function." --How can characters already put into the buffer be discarded?One approach is that the original characters that were removed are "remembered",and each new character that was put in is identified and replaced with original character.But it seems very inefficient.The other option is to load a copy of the original file into buffer and place the file pointer at the intended position.Which approach of these two does fseek, fsetpos or rewind take to discard the characters put using ungetc()?

有关文本流,如何流中的未读字符,字符被投入使用的presence 与ungetc(),影响收益值 FTELL()?我的困惑来自对以下行出现 FTELL()与ungetc()从约 FTELL 此链接(的 SOURCE

For text streams,how does the presence of unread characters in the stream,characters that were put in using ungetc(), affect the return value of ftell()?My confusion arise from the following line about ftell() and ungetc() from this link about ftell(SOURCE)

对于文本流,数值可能没有意义,但仍可以使用的位置还原到同一位置以后使用fseek的(如果有字符放回去使用与ungetc仍然是悬而未决阅读,这种行为是未定义)。


  • 围绕上述段落的最后一行,什么也的读取挂起要做一个与ungetc() - 获得字符被丢弃?我们每次阅读使用放入流的字符与ungetc(),是的读取后丢弃

  • Focusing on the last line of the above paragraph,what has pending of being read got to do with a "ungetc()-obtained" character being discarded? Each time we read a character that was put into the stream using ungetc(),is it discarded after the read?

推荐答案

在放回性格良好的精神模型只是它的其中挂关闭一些额外的小产权FILE * 对象。想象一下,你有:

A good mental model of the put back character is simply that it's some extra little property which hangs off the FILE * object. Imagine you have:

typedef struct { 
   /* ... */
   int putback_char;
   /* ... */
} FILE;

想象 putback_char 初始化值 EOF ,表示没有补篮字符,而与ungetc 只存储的字符给这个会员。

Imagine putback_char is initialized to the value EOF which indicates "there is no putback char", and ungetc simply stores the character to this member.

想象一下,每一个读操作经过 GETC ,而 GETC 确实是这样的:

Imagine that every read operation goes through getc, and that getc does something like this:

int getc(FILE *stream)
{
   int ret = stream->putback_char;

   if (ret != EOF) {
     stream->putback_char = EOF;
     if (__is_binary(stream))
        stream->current_position--;
     return ret;
   }

   return __internal_getc(stream); /* __internal_getc doesn't know about putback_char */
}

从而清除推回的功能只是将 EOF putback_char

在换句话说,放回字符(并且只有一个需要被支持的)实际上可以是一个微型缓冲器是从正规缓冲分开。 (考虑,即使是无缓冲流支持与ungetc :这样的流具有把字节或字符的地方)

In other words, the put back character (and only one needs to be supported) can actually be a miniature buffer which is separate from the regular buffering. (Consider that even an unbuffered stream supports ungetc: such a stream has to put the byte or character somewhere.)

对于位置指示器,C99标准这样说:

Regarding the position indicator, the C99 standard says this:

有关文本流,直到所有推回字符阅读或废弃的文件位置指示器的成功调用与ungetc 函数后的值是不确定的。对于二进制流,它的文件位置指示器由每个成功调用与ungetc 函数递减;如果它的值在通话之前为零,这是在调用之后不确定。 [7.19.7.11的与ungetc 功能]

For a text stream, the value of its file position indicator after a successful call to the ungetc function is unspecified until all pushed-back characters are read or discarded. For a binary stream, its file position indicator is decremented by each successful call to the ungetc function; if its value was zero before a call, it is indeterminate after the call. [7.19.7.11 The ungetc function]

所以,www.cplusplus.com引用您正在使用不正确; FTELL 的行为是不是不确定的,当有挂起字符推回用与ungetc
对于文本流,价值是不确定的。访问未指定的值不是不确定的行为,因为一个未确定的值不能再presentation一个陷阱。
存在二进制流的未定义的行为,如果一个逼退发生在零的位置,因为位置就变成了不确定的。不确定意味着它是一个未确定的值可能是重新presentation一个陷阱。访问它可以用一个错误消息停止程序,或触发其他行为。

So, the www.cplusplus.com reference you're using is incorrect; the behavior of ftell is not undefined when there are pending characters pushed back with ungetc. For text streams, the value is unspecified. Accessing an unspecified value isn't undefined behavior, because an unspecified value cannot be a trap representation. The undefined behavior exists for binary streams if a push back occurs at position zero, because the position then becomes indeterminate. Indeterminate means that it's an unspecified value which could be a trap representation. Accessing it could halt the program with an error message, or trigger other behaviors.

这是更好地得到马的嘴编程语言和库规范,而不是从随机网站。

It's better to get programming language and library specifications from the horse's mouth, rather than from random websites.

这篇关于撤消与ungetc()的作用:"如何" fseek的做(),快退()和fsetpos()做呢?是缓冲每次重新填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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