ANSI C 中的无缓冲 I/O [英] Unbuffered I/O in ANSI C

查看:21
本文介绍了ANSI C 中的无缓冲 I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了教育和编程实践,我想编写一个简单的库,可以处理原始键盘输入,并实时"输出到终端.

For the sake of education, and programming practice, I'd like to write a simple library that can handle raw keyboard input, and output to the terminal in 'real time'.

我想尽可能地坚持使用 ansi C,我只是不知道从哪里开始这样的事情.我已经做了几次谷歌搜索,99% 的结果使用了库,或者用于 C++.

I'd like to stick with ansi C as much as possible, I just have no idea where to start something like this. I've done several google searches, and 99% of the results use libraries, or are for C++.

我真的很想让它在 Windows 中运行,然后在我有时间的时候将它移植到 OSX.

I'd really like to get it working in windows, then port it to OSX when I have the time.

推荐答案

尽可能坚持使用标准 C 是一个好主意,但仅使用标准 C 无法完成所采用的任务.机制一次从终端获取一个字符本质上是特定于平台的.对于 POSIX 系统 (MacOS X),请查看 <termios.h> 标题.较旧的系统使用种类繁多的标头和系统调用来实现类似的效果.您必须决定是否要进行任何特殊字符处理,记住诸如line kill"之类的东西会出现在行尾并清除目前为止输入的所有字符.

Sticking with Standard C as much as possible is a good idea, but you are not going to get very far with your adopted task using just Standard C. The mechanisms to obtain characters from the terminal one at a time are inherently platform specific. For POSIX systems (MacOS X), look at the <termios.h> header. Older systems use a vast variety of headers and system calls to achieve similar effects. You'll have to decide whether you are going to do any special character handling, remembering that things like 'line kill' can appear at the end of the line and zap all the characters entered so far.

对于 Windows,您需要深入研究 WIN32 API - Unix 和 Windows 之间的代码基本上没有共性,至少在您将终端"置于逐字符模式的地方是这样.一旦您有了读取单个字符的机制,您就可以管理通用代码 - 很可能.

For Windows, you'll need to delve into the WIN32 API - there is going to be essentially no commonality in the code between Unix and Windows, at least where you put the 'terminal' into character-by-character mode. Once you've got a mechanism to read single characters, you can manage common code - probably.

此外,您还需要担心字符和按下的键之间的差异.例如,要在 MacOS X 上输入ï",请键入 option-ui.这是三个按键.

Also, you'll need to worry about the differences between characters and the keys pressed. For example, to enter 'ï' on MacOS X, you type option-u and i. That's three key presses.

这篇关于ANSI C 中的无缓冲 I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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