5

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 Answers 2

3

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

2
  • why readText is placed outside std.file? Could you explain? Mar 5, 2019 at 6:51
  • It isn't - readText is in std.file. File, however, is in std.stdio, and I can see how that can be confusing.
    – BioTronic
    Mar 5, 2019 at 7:39
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.