ncurses的,打印和当代获取字符串 [英] ncurses, print and contemporary acquire strings

查看:146
本文介绍了ncurses的,打印和当代获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C语言编写的应用程序的ncurses 。 A ,而循环使用,如果有新邮件到达一个队列,不断检查:如果是这样,印在屏幕上的消息,然后从队列中删除:

A program written in C uses ncurses. A while cycle is used to continuously check if a new message is arrived in a queue: if it is, the message is printed on screen and then removed from the queue:

while (condition)
{
if (queue_not_empty)
{
printw(element_from_queue);
refresh();
remove(element_from_queue);
}
}

目前相同的时间,但是,该方案应能从用户获得的输入字符串,然后将其存储在数组炭消息[100] 在通过一 scanw
但是,如果我把

At the same time, however, the program should be able to acquire an input string from the user and then store it in the array char message[100] through a scanw. But if I put

while (condition)
{
if (queue_not_empty)
{
printw(element_from_queue);
refresh();
remove(element_from_queue);
}
scanw(message);
}

该周期将停止,直到用户没有输入一个字符串,程序将打印用户输入后,队列最多的新消息。它不应该是这个样子!队列中的消息可以到达在任何时间和应打印;用户的消息可以在任何时间到达和应该存储到阵列

the cycle will stop until the user doesn't type a string and the program will print the new messages of the queue only after a user input. It should not be like this! The queue messages could arrive at any time and should be printed; the user messages could arrive at any time and should be stored into the array.

我想避免另一个线程的创建过程,因为的ncurses 变得怪异多线程。无论如何,我需要两个当代,而周期,一个用于印刷的消息和一个用于读取用户输入。

I would like to avoid the creation of another thread, because ncurses becomes weird with multiple threads. Anyway, I would need two "contemporary" while cycles, one for printing the messages and one for reading the user input.

,愿你有一个解决方案?

May there be a solution?

在换句话说:这可能与的ncurses 来打印一些输出,并得到一些来自用户的多个字符输入同屏在同一个线程

In other words: is it possible with ncurses to print some output and to get some multiple characters input from the user in the same screen, in the same thread?

推荐答案

ncurses的提供了用于阅读的的角色与的超时(甚至的无延迟在所有)。那些不符合 scanw 有用(它总是被阻塞,等待的若干个的字符):

ncurses provides functions for reading a single character with a timeout (or even no delay at all). Those are not useful with scanw (which always blocks, waiting for several characters):

  • when using a timeout, getch returns ERR rather than a character when the timeout expires.
  • scanw may use getch internally, but has no way to continue when getch returns ERR.

而不是的,你可以的调查的输入。如果无字符是一个给定的时间间隔内可用,程序放弃(暂时),并做了不是等待字符更为有用。
该民意调查输入可以写入接受字符一次一个应用程序,将它们添加到一个缓冲区,直到用户presses <大骨节病>输入和然后的运行
的sscanf 在已完成的文本。

Rather than block, you can poll for input. If no character is available within a given time interval, your program gives up (for the moment), and does something more useful than waiting for characters. Applications that poll for input can be written to accept characters one at a time, adding them to a buffer until the user presses Enter, and then run sscanf on the completed text.

在常规的C 的sscanf 使用(从读取的字符串的),而不是直接从的屏幕阅读的与 scanw

The regular C sscanf is used (which reads from a string) rather than reading directly from the screen with scanw.

这篇关于ncurses的,打印和当代获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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