如何在标准输入缓冲区,并在连续调用的getchar()指针的变化? [英] How does the STDIN buffer and getchar() pointer change during successive calls?

查看:236
本文介绍了如何在标准输入缓冲区,并在连续调用的getchar()指针的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准输入缓冲区给定的输入,当连续调用的getchar()被执行,确实沿标准输入缓冲区的内存地址指针移动,这样的getchar()来检索每个地址的值?如果是这样,一旦被取回正在所述值除去指针然后递增<​​/ P>

通常我的getchar()在一个循环的理解如下这样的逻辑:


  1. 的getchar()被称为

  2. 标准输入缓冲区检查输入

  3. 如果标准输入缓冲区为空的getchar()睡

  4. 用户输入输入和唤醒获得CHAR()

  5. 标准输入缓冲区再次检查输入

  6. 标准输入缓冲区不为空

  7. 的getchar()的标准输入缓冲区的开始检索地址值

  8. 在地址值从标准输入缓冲区取出,指针递增

  9. 后续调用重复步骤7-8,直到遇到EOF

有一个类似的问题被问过关于<一个href=\"http://stackoverflow.com/questions/22444932/confusion-about-how-a-getchar-loop-works-internally\">stackoverflow但我无法理解的反应。


解决方案

一般是有标准输入输出内部缓冲区。的getchar()可以触发读入缓冲器中的行,通常在随后的调用,它将简单直到指针到达在缓冲器中的当前数据的末尾增加一个指针。实现通常使用一个简单的内部的char *动态内存的基本块,有几个指针和状态变量(S)。

实现有所不同,我不记得了POSIX标准意味着太多关于内部实现的getchar()或一般标准输入输出流,但鉴于操作应予以支持。

如果我还记得,一些实现的无缓冲(我想在DOS编译器我用没有缓冲),但也有多个标准库实现对给定的操作系统。

这并非罕见有在同一系统上2标准输入输出库,例如:SYS-管理员管理于AIX,Solaris,HPUX,以及其他非Linux / BSD UNIX平台上经常会安装GNU栈获得的工具,如GCC,而栈包含的glibc(GNU库)。

您可以在网上下载的libc /标准输入输出源。见glibc的。

如果有帮助,认为标准输入输出提供了PEEK和unget功能,并做到这一点的唯一方法是通过终端与用户程序之间的内部缓冲区。

Given input in the stdin buffer, when successive calls to getchar() are performed, does the pointer move along the memory address of the stdin buffer, allowing getchar() to retrieve the value at each address? If so, once they have been retrieved are the values removed and the pointer then incremented?

Generally my understanding of getchar() in a loop follows this logic:

  1. getchar() called
  2. stdin buffer checked for input
  3. If stdin buffer empty, getchar() sleeps
  4. user enters input and awakens get char()
  5. stdin buffer checked again for input
  6. stdin buffer not empty
  7. getchar() retrieves value at address at the start of the stdin buffer
  8. value at address removed from stdin buffer, pointer incremented
  9. subsequent calls repeat steps 7-8 until EOF encountered

A similar question was asked before on stackoverflow but I had trouble understanding the responses.

解决方案

Generally there is a stdio internal buffer. getchar() may trigger a line read into the buffer, and generally on subsequent calls, it will simply increment a pointer until the pointer reaches the end of the current data in the buffer. The implementation usually uses a simple internal char * to an underlying chunk of dynamic memory, with a few pointers and state variable(s).

Implementations vary, I don't recall the POSIX standard implying much about the internal implementation of getchar() or stdio streams in general, except that given operations should be supported.

If I recall, some implementations are unbuffered (I think the DOS compiler I used did not buffer), but there are multiple standard lib implementations for a given OS.

It is not uncommon to have 2 stdio libs on the same system, example: sys-admins managing AIX, Solaris, HPUX, and other non-Linux/BSD UNIX platforms will frequently install the GNU stack to get tools like gcc, and that stack includes glibc (GNU LIBC).

You can download a libc/stdio source online. See glibc.

If it helps, consider that stdio provides peek and unget functionality, and the only way to do that is by an internal buffer between the terminal and the user program.

这篇关于如何在标准输入缓冲区,并在连续调用的getchar()指针的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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