In C++, istream& operator>> can be used to read data “as in text”. What is the equivalent in D?
My attempt:
input.txt
c 1033
90.432
input_test.d
import std.stdio;
import std.stream;
void main()
{
auto inputFile = new BufferedFile("input.txt");
char c;
int i;
double d;
inputFile.read(c);
inputFile.read(i);
inputFile.read(d);
writeln(c, '\t', i, '\t', d);
}
Output
c 858796320 4.90559e-62
D has lots of ways of reading data from files to make various use cases convenient. Here are some:
Based on your specific case, you’ll probably want to use slurp or readf. Your other option is to read lines and split them into the fields you want, then use std.conv.to to parse the textual representation:
In summary, if every line has the same format, slurp is the nicest way to go. Otherwise, you’ll have to decide what will be most convenient for you.