在C中输入后没有换行符 [英] No newline after input in C

查看:143
本文介绍了在C中输入后没有换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中请求用户输入后,如何阻止给定应用创建换行符('\ n')? 我想要类似的东西:

How can I prevent a given app from creating a newline ('\n') after I request user input in C? I'd like something like:

Type a number
Number: 3x10 = 30

用户输入数字后,将添加"x10 = 30".问题是我不能在同一行上执行(并且我想这样做).

The "x10 = 30" is added after the user inputs the number.. The problem is I can't do it on the same line (and I'd like to do it).

有人可以帮助我吗?

推荐答案

正如KillianDS所说,大多数终端会自动缓冲输入,因此您必须告诉终端接口需要原始/未缓冲的输入.不幸的是,在大多数系统之间,执行此操作的方法会有所不同.

As KillianDS said, most terminals buffer input automatically so you have to tell the terminal interface that you want raw/unbuffered input. Unfortunately, the way to do this will vary between most systems.

在UN * X系统上,签出stty命令(例如,stty raw设置为无缓冲,而stty sane设置为恢复正常,可以通过systemexecl系列调用它们).您也可以从termios界面使用tcsetattr命令.

On UN*X systems, check out the stty command (e.g. stty raw to set unbuffered and stty sane to go back to normal, these can be called via system or the execl family). You could also use the tcsetattr command from the termios interface.

这篇关于在C中输入后没有换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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