5
votes

I am trying to use the readText function:

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;
}

I am getting an error:

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)))

What am I doing wrong?

2

2 Answers

3
votes

readText takes a string argument, being the file name to read from. Since you've opened the file with File(xmlName, "r"), you should be using the methods defined in std.stdio.File.

It seems what you want is to read the entirety of the file contents into a string. In this case, I suggest replacing the two last lines in your main function with string file_text = readText(xmlName);

0
votes

Since you want to use the std.file's readText(), you need to slighly modify your code:

import std.stdio;
import std.file;

string xmlName = "D:/files/123.xml";

void main() {
  writeln("Edit source/app.d to start your project.");
  string file_text = file.readText(xmlName);
}

Notice that we no longer create an instance of a File as it is not needed here...