load data from txt file
Tony
tonytdominguez at aol.com
Wed Jan 3 05:45:32 UTC 2018
On Tuesday, 2 January 2018 at 22:08:52 UTC, aerto wrote:
> Hello and happy new year im new in d so i have a question
>
> i have into a txt file named users.txt the bellow
>
> ["admin":"123456789"]
> ["test":"test345"]
>
>
> im my app
>
> string[string] data;
>
> so i need to load users.txt content into data in order to be
> able to run
>
>
> writeln(data["admin"]); // i want this to print 123456789
> writeln(data["test"]); // i want this to print test345
This seems to work (on Linux with no error checking) but is using
the deprecated removechars() which gets deleted May 2018. There
should be a simple fix using std.regex.replaceAll but I can't
even get a successful compile right now (templates aren't
deducing). Someone else should know what the correct replacement
is for removechars().
import std.stdio;
import std.string;
import std.algorithm;
void main()
{
string line;
string[string] data;
auto f = File("users.txt","r");
while ((line = f.readln('\n')) !is null)
{
string trimmed = removechars!string(line,"[\\[\\]\"\n\r]");
auto fields = findSplit(trimmed,":");
data[fields[0]] = fields[2];
}
writeln(data);
writeln("data for key admin:",data["admin"]);
f.close();
}
More information about the Digitalmars-d
mailing list