特殊的 writef/writefln 行为?

发布于 2024-12-25 01:51:25 字数 1601 浏览 0 评论 0原文

我已经看了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2025-01-01 01:51:25

在调用 writewritef 后使用 stdout.flush();。后面的这些调用不会刷新缓冲区,这就是您看到此行为的原因。顺便说一句,getch 不在 std.c.stdio 中(至少不在 D2 中?),它位于 DMC 的 CRT 库 (SNN.lib) 中,要正确使用它,您需要必须将其原型设计为 extern (C) int getch();

extern (C) int getch();
import std.process; /* for system() */
import std.stdio; /* for writefln */

void main() { 
    writef("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
    stdout.flush();
    getch();

    writef("Waiting again\n(using 'system(\"pause\");'):");
    stdout.flush();
    system("pause");
}

但由于 getch(),这不是跨平台兼容的。如果您想使用更好的用户输入工具,您可以查看 Jesse 的 cmdln 库: https://github.com/he-the-great/JPDLibs/tree/cmdln。它有一个相当酷的界面:

auto num = require!(int, "a > 0 && a <= 10")("Enter a number from 1 to 10");

Use stdout.flush(); after any calls to write or writef. These latter calls don't flush the buffer, which is why you're seeing this behavior. Btw getch is not in std.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 as extern (C) int getch();:

extern (C) int getch();
import std.process; /* for system() */
import std.stdio; /* for writefln */

void main() { 
    writef("Press a key (using 'std.c.stdio.getch();' to wait) . . .");
    stdout.flush();
    getch();

    writef("Waiting again\n(using 'system(\"pause\");'):");
    stdout.flush();
    system("pause");
}

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:

auto num = require!(int, "a > 0 && a <= 10")("Enter a number from 1 to 10");
泪意 2025-01-01 01:51:25

gmane.comp.lang 上的此帖子.d.learn 似乎指示 writef 仅在遇到换行符时刷新输出。由于 writefln 是对 writef 的简单调用,并附加了换行符,因此 writefln 始终刷新输出。最后一个换行符之后的所有文本都会被缓冲,直到程序结束。

This thread on gmane.comp.lang.d.learn seems to indicate that writef only flushes the output when it encounters a newline. Since writefln is a simple call to writef with a newline appended, writefln always flushes the output. All text after the last newline is buffered until the end of the program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文