直到给出换行符时,输出才显示为非睡眠状态 [英] Output not displayed with usleep until a line break is given
本文介绍了直到给出换行符时,输出才显示为非睡眠状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在C语言中编写一个简单的打字机"效果,其中文本一次出现一个字母,并有一定延迟.这是我的功能:
I'm trying to program a simple "typewriter" effect in C, where text appears one letter at a time with a delay. Here's the function I have:
#include <stdio.h>
#include <unistd.h>
void typestring(const char *str, useconds_t delay)
{
while (*str) {
putchar(*(str++));
usleep(delay);
}
}
问题是直到显示\n
,文本才真正出现.我在做什么错了?
The problem is the text doesn't actually appear until a \n
is displayed. What am I doing wrong?
推荐答案
缓冲到stdout
的输出.使用\n
强制刷新.如果要更改此设置,则需要更改终端的设置(对于Linux,请在此处)或使用
The output to stdout
is buffered. Using \n
you are forcing a flush. If you want to change this, you will need to change the settings of the terminal (for Linux look here) or use
void typestring(const char *str, useconds_t delay)
{
while (*str) {
putchar(*(str++));
fflush(stdout);
usleep(delay);
}
}
这篇关于直到给出换行符时,输出才显示为非睡眠状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文