模板 std.file.readText 不能从参数类型推导出函数!()(File)

Dmi*_*kov 5 d

我正在尝试使用readText函数:

import std.stdio;
import std.file;

string xmlName = r"D:\files\123.xml";
File file;

void main()
{
    writeln("Edit source/app.d to start your project.");
    file = File(xmlName, "r");
    string file_text = file.readText;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Error: template std.file.readText cannot deduce function from argument types !()(File), candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\file.d(499,3):        std.file.readText(S = string, R)(auto ref R name) if (isSomeString!S && (isInputRange!R && !isInfinite!R && isSomeChar!(ElementType!R) || is(StringTypeOf!R)))
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 3

readText接受一个字符串参数,作为要读取的文件名。由于您已使用 打开文件File(xmlName, "r"),因此您应该使用 中定义的方法std.stdio.File

看来您想要的是将整个文件内容读入字符串中。在这种情况下,我建议将函数中的最后两行替换mainstring file_text = readText(xmlName);