load data from txt file
Muld
2 at 2.2
Wed Jan 3 02:36:54 UTC 2018
On Tuesday, 2 January 2018 at 23:25:21 UTC, visitor wrote:
> 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
>>
>> writeln(data["admin"]); // i want this to print 123456789
>> writeln(data["test"]); // i want this to print test345
>
> if you can rewrite your users.txt file like so :
>
> ["admin":"123456789",
> "test":"test345"]
>
> with the help of import expression /-J command line switch (or
> dub stringImportPaths)
> https://dlang.org/spec/expression.html#import_expressions
> "dmd -release -J. yourfile.d" for example if users.txt is in
> same folder
>
> import std.stdio;
>
> string[string] data;
>
> static this() {
> // of course you could also do this in main()
> data = mixin(import("users.txt"));
> }
>
> void main(string[] args)
> {
> writefln("test => %s", data["test"]);
> }
This means you need to recompile your code any time you want to
change users.txt. Which would not be ideal from a user's
perspective of the program.
More information about the Digitalmars-d
mailing list