与ungetc:一些推回的字节 [英] ungetc: number of bytes of pushback

查看:138
本文介绍了与ungetc:一些推回的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与ungetc只保证采取推回的一个字节。在另一方面,我测试过它在Windows和Linux,它似乎有两个字节来工作。

ungetc is only guaranteed to take one byte of pushback. On the other hand, I've tested it on Windows and Linux and it seems to work with two bytes.

是否有关于它实际上只占用一个字节的平台(例如任何当前的Unix系统)?

Are there any platforms (e.g. any current Unix systems) on which it actually only takes one byte?

推荐答案

C99标准(而在这之前的C89标准)毫不含糊地说:

The C99 standard (and the C89 standard before that) said unequivocally:

推回一个字符的保证。如果与ungetc 函数被调用太多
  在同流次无上插入读取或文件定位操作
  流,该操作可能会失败。

One character of pushback is guaranteed. If the ungetc function is called too many times on the same stream without an intervening read or file positioning operation on that stream, the operation may fail.

所以,要随身携带,你不要以为飞机推出的多个字符。

So, to be portable, you do not assume more than one character of pushback.

话虽如此,两个的MacOS X 10.7.2(狮子)和RHEL 5(Linux操作系统,86/64),我尝试:

Having said that, on both MacOS X 10.7.2 (Lion) and RHEL 5 (Linux, x86/64), I tried:

#include <stdio.h>
int main(void)
{
    int i;
    for (i = 0; i < 4096; i++)
    {
        int c = i % 16 + 64;
        if (ungetc(c, stdin) != c)
        {
            fprintf(stderr, "Error at count = %d\n", i);
            return(1);
        }
    }
    printf("No error up to count = %d\n", i-1);
    return(0);
}

我得到了两种平台上没有错误。与此相反,在Solaris 10(SPARC),我在'数= 4'得到了一个错误。更糟的是,在HP-UX 11.00(PA-RISC)和HP-UX 11.23(安腾),我在'数= 1'得到一个错误 - belying的理论,2是安全的。同样,AIX 6.0在了一个错误'计数= 1'。

I got no error on either platform. By contrast, on Solaris 10 (SPARC), I got an error at 'count = 4'. Worse, on HP-UX 11.00 (PA-RISC) and HP-UX 11.23 (Itanium), I got an error at 'count = 1' - belying the theory that 2 is safe. Similarly, AIX 6.0 gave an error at 'count = 1'.


  • 的Linux:大(4昆明植物研究所)

  • MAXOS X:大(4昆明植物研究所)

  • 的Solaris:4

  • HP-UX:1

  • AIX:1

所以,AIX和HP-UX只允许上一直没有阅读它的任何数据输入文件推回一个字符。这是一个讨厌的情况;他们可能会提供更多的容量后推一旦某些数据已经从文件(但在AIX上一个简单的测试增加了的getchar()读前循环并没有改变推回容量)。

So, AIX and HP-UX only allow one character of pushback on an input file that has not had any data read on it. This is a nasty case; they might provide much more pushback capacity once some data has been read from the file (but a simple test on AIX adding a getchar() before the loop didn't change the pushback capacity).

这篇关于与ungetc:一些推回的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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