特殊的 writef/writefln 行为?
我已经看了 D 大约 15 分钟,所以难怪我有疑问,但一些奇怪的事情发生在我身上。
我从 此处 安装了 D,并从 此处,我在 Visual Studio 2010 Professional 中运行所有内容。 D 示例编译并运行,调试器似乎工作正常。
在浏览dsource.org 的基础教程时,我正在阅读writef 而不是 writefln
然后输出的最后一行在暂停后打印。
这是示例的代码:
import std.c.stdio; /* for getch() */
import std.process; /* for system() */
import std.stdio; /* for writefln */
void main() {
writefln("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
getch();
writefln("Waiting again\n(using 'system(\"pause\");'):");
system("pause");
}
这是我的代码,请注意,唯一的更改是将 writefln
更改为 writef
import std.c.stdio; /* for getch() */
import std.process; /* for system() */
import std.stdio; /* for writefln */
void main() {
writef("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
getch();
writef("Waiting again\n(using 'system(\"pause\");'):");
system("pause");
}
使用 writef
程序将在屏幕上不显示任何内容,暂停在 getch
处,然后当我按下某个键时,我会看到提示:
Press a key (using 'std.c.stdio.getch();' to wait) . . .Waiting again
Press any key to continue . . .
但不是“(using 'system("pause");'):”。当我按下控制台中的“暂停”命令时,会出现括号语句。如果我使用 writefln ,它会打印、等待、打印两行,然后按照您的预期再次等待。
如何解释这种行为?
So I've been looking at D for about 15 mins, so it's no wonder I have questions, but something strange is happening for me.
I installed D from here and Visual D from here and I'm running everything in Visual Studio 2010 Professional. D examples compile and run and the debugger appears to be working fine.
While going through dsource.org's fundamental tutorials, I was reading the Wait section when I noticed that if you use writef
instead of writefln
then the last line of the output prints after the pause.
Here's the example's code:
import std.c.stdio; /* for getch() */
import std.process; /* for system() */
import std.stdio; /* for writefln */
void main() {
writefln("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
getch();
writefln("Waiting again\n(using 'system(\"pause\");'):");
system("pause");
}
And here's mine, note the only change is writefln
to writef
import std.c.stdio; /* for getch() */
import std.process; /* for system() */
import std.stdio; /* for writefln */
void main() {
writef("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
getch();
writef("Waiting again\n(using 'system(\"pause\");'):");
system("pause");
}
With writef
the program will display nothing on the screen, pause at getch
, then when I press a key I see the prompt:
Press a key (using 'std.c.stdio.getch();' to wait) . . .Waiting again
Press any key to continue . . .
but NOT "(using 'system("pause");'):". The parenthetic statement appears after I press a key to get through the 'pause' command in the console. If I use writefln
it prints, waits, prints both lines, then waits again as you'd expect.
What explains this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用
write
或writef
后使用stdout.flush();
。后面的这些调用不会刷新缓冲区,这就是您看到此行为的原因。顺便说一句,getch
不在std.c.stdio
中(至少不在 D2 中?),它位于 DMC 的 CRT 库 (SNN.lib) 中,要正确使用它,您需要必须将其原型设计为extern (C) int getch();
:但由于
getch()
,这不是跨平台兼容的。如果您想使用更好的用户输入工具,您可以查看 Jesse 的 cmdln 库: https://github.com/he-the-great/JPDLibs/tree/cmdln。它有一个相当酷的界面:Use
stdout.flush();
after any calls towrite
orwritef
. These latter calls don't flush the buffer, which is why you're seeing this behavior. Btwgetch
is not instd.c.stdio
(at least not in D2?), it's in DMC's CRT library (SNN.lib), and to properly use it you would have to prototype it asextern (C) int getch();
:But this is not cross-platform compatible due to
getch()
. If you want to use a nicer user-input facility you can check out Jesse's cmdln library: https://github.com/he-the-great/JPDLibs/tree/cmdln. It has a rather cool interface:gmane.comp.lang 上的此帖子.d.learn
似乎指示writef
仅在遇到换行符时刷新输出。由于writefln
是对writef
的简单调用,并附加了换行符,因此writefln
始终刷新输出。最后一个换行符之后的所有文本都会被缓冲,直到程序结束。This thread on
gmane.comp.lang.d.learn
seems to indicate thatwritef
only flushes the output when it encounters a newline. Sincewritefln
is a simple call towritef
with a newline appended,writefln
always flushes the output. All text after the last newline is buffered until the end of the program.