如何获得与ungetc放回到一个文件最后一次字符* [英] How to get the character last time ungetc puts back into a FILE*
问题描述
假设我有,如果我称之为 FILE *
按 fopencookie的
创建的,会发生什么与ungetc
在 FILE *
?假设我有 setvbuf用来
在 FILE *
为 _IONBF
使其无缓冲。貌似fopencookie的不支持的处理程序与ungetc
。
Suppose I have a FILE*
created by fopencookie
, what happens if I call ungetc
on the FILE*
? Suppose I've setvbuf
on the FILE*
as _IONBF
to make it unbuffered. Looks like the fopencookie doesn't support a handler for ungetc
.
的背景是我想要运行 scanf函数
在的IStream
,看到的scanf~~V上IStream对象。一个建议是使用 fopencookie的
和我试图总结的IStream
的FILE *通过 fopencookie的
,问题出来就是,我需要确保的 FILE *
不缓冲任何东西,然而, scanf函数
解析其宽度是未知的领域,例如,当将至少读取一个字符前面%d位将继续读,直到找到空间或结束行。我假设 scanf函数
将把字符回 FILE *
按与ungetc
,在这种情况下,我需要把这个字符回的IStream
为好。是否有可能知道哪些字 scanf函数
ungets?谢谢你。
The background is I want to run scanf
on istream
, see scanf on an istream object. One suggestion is to use fopencookie
and I was trying to wrap istream
as FILE* by fopencookie
, the problem comes out is, I need to make sure the FILE*
doesn't buffer anything, however, scanf
will at least read one character ahead when parsing fields whose width is unknown, e.g. for %d it will continue to read until find space or end-of-line. I assume scanf
will put the character back to the FILE*
by ungetc
, in which case I need to put this character back to the istream
as well. Is it possible to know which character scanf
ungets? Thanks.
推荐答案
fopencookie的
是GNU扩展它,因为据我所知,存在于任何其他C库。
fopencookie
is a GNU extension which, as far as I know, exists in no other C library.
在GNU libc实现的 STDIO
是断然多毛比它应该是因为一个古老的(pre-EGCS,如果这意味着什么你)尝试使文件
物品直接可用作C ++ 流缓冲
对象 - 没有愚弄! - 但回答你的问题可以在这里找到:的 https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/genops.c#l722 (与ungetc <实施/ code>被命名为
_IO_sputbackc
是古代尝试其余遗迹之一),我会引用code,因为它很短:
The GNU libc implementation of stdio
is decidedly hairier than it ought to be because of an ancient (pre-EGCS, if that means anything to you) attempt to make FILE
objects directly usable as C++ streambuf
objects - no foolin'! - but the answer to your question may be found here: https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/genops.c#l722 (the implementation of ungetc
being named _IO_sputbackc
is one of the remaining vestiges of that ancient attempt) I'll quote the code, it's short:
722 int
723 _IO_sputbackc (fp, c)
724 _IO_FILE *fp;
725 int c;
726 {
727 int result;
728
729 if (fp->_IO_read_ptr > fp->_IO_read_base
730 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
731 {
732 fp->_IO_read_ptr--;
733 result = (unsigned char) c;
734 }
735 else
736 result = _IO_PBACKFAIL (fp, c);
737
738 if (result != EOF)
739 fp->_flags &= ~_IO_EOF_SEEN;
740
741 return result;
742 }
什么此的意思是,如果字符被推回相同权在 FILE
对象的缓冲区读指针背后的字符,它只是递减读指针一次;否则, _IO_PBACKFAIL
被调用。围绕略偏在该目录中挖掘(特别是,在 libioP.h
和的 iofopncook.c
)显示, _IO_PBACKFAIL
在文件
与 fopencookie的
调用的 _IO_default_pbackfail
这功能是更长的时间,我不会引用它,但它的作用是分配一个特殊的备份缓冲区而其余的人物在那里。此备份缓冲区可以增长必要时以适应呼叫到与ungetc
任意号码。
What this is saying is that if the character pushed back is identical to the character right behind the read pointer in the FILE
object's buffer, it just decrements the read pointer once; otherwise, _IO_PBACKFAIL
is called. Digging around a bit more in that directory (in particular, in libioP.h
and iofopncook.c
) reveals that _IO_PBACKFAIL
on a FILE
created with fopencookie
calls _IO_default_pbackfail
That function is much longer and I'm not going to quote it, but what it does is allocate a special "backup buffer" and stash the character there. This backup buffer can grow to accomodate an arbitrary number of calls to ungetc
if necessary.
所以,回答你的问题是,无论多少次,你叫与ungetc
成一排(或其他库函数为你做),你的cookie功能永远不会要求做任何事情。这一切都处理内部的文件
。按我的回答你的其他问题,我强烈建议你找一些其他的办法,但对你的是的尝试做的,我不明白为什么你认为你的需要把这个字符回的IStream
为好。
So, the answer to your question is that no matter how many times you call ungetc
in a row (or other library functions do it for you), your cookie functions will never get asked to do anything. It's all handled internal to the FILE
. Per my answer to your other question, I strongly advise that you find some other approach, but for what you were trying to do, I don't see why you think you need to "put this character back to the istream
as well."
这篇关于如何获得与ungetc放回到一个文件最后一次字符*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!