目前我知道如何在D中输入的唯一方法是使用scanf()函数。但该死的,该死的很难看。你会认为,因为它是从C升级的,所以他们会修复它。
我正在寻找一种方法来用一个参数来做。目前你必须这样做:
int foo = 0;
scanf("%i", &foo);
writeln("%i", foo);
但是用一个参数看起来会更清晰。类似的东西:
int foo = 0;
scanf(foo);
writeln(foo);
感谢。
答案 0 :(得分:9)
readf("%d", &foo);
允许使用std.stdio.File
而不是C FILE*
foo = readln().strip().to!int();
int[] numbers = slurp!int("filename", "%d");
答案 1 :(得分:6)
这里有一个非常酷的用户输入模块:
https://github.com/Abscissa/scriptlike/blob/master/src/scriptlike/interact.d
示例代码:
if (userInput!bool("Do you want to continue?"))
{
auto outputFolder = pathLocation("Where you do want to place the output?");
auto color = menu!string("What color would you like to use?", ["Blue", "Green"]);
}
auto num = require!(int, "a > 0 && a <= 10")("Enter a number from 1 to 10");
答案 2 :(得分:4)
以上答案很棒。我只想加2美分。
我经常有以下简单的功能:
T read(T)()
{
T obj;
readf(" %s", &obj);
return obj;
}
它是通用的,非常方便 - 它可以吞噬任何空白区域并读取您要求的任何类型。您可以像这样使用它:
auto number = read!int;
auto floating_number = read!float;
// etc.