digitalmars.D.learn - Read file on compiler time.
- Remo (4/4) May 29 2014 Is there a way to read a text file into a sting at compile time
- Adam D. Ruppe (5/5) May 29 2014 string a = import("file.txt");
- Remo (6/11) May 29 2014 Excellent, thank you Adam!
- Adam D. Ruppe (25/27) May 29 2014 Sort of, use
- bearophile (6/7) May 29 2014 There is pragma(msg, "...") but it's a little crappy. There are
- Remo (6/13) May 29 2014 Thanks!
Is there a way to read a text file into a sting at compile time in D2 ? It would be great to read for example some JSON file and then parse it using CTFU and create some D code based on it.
May 29 2014
string a = import("file.txt"); dmd yourprogram.d -Jlocation_of_file so for example dmd yourprogram.d -J. if file.txt is in the same directory as the .d file.
May 29 2014
On Thursday, 29 May 2014 at 20:21:32 UTC, Adam D. Ruppe wrote:string a = import("file.txt"); dmd yourprogram.d -Jlocation_of_file so for example dmd yourprogram.d -J. if file.txt is in the same directory as the .d file.Excellent, thank you Adam! Now another question is it also possible to save/write string at compile time? Probably no... Any way I like CTFE !
May 29 2014
On Thursday, 29 May 2014 at 20:38:30 UTC, Remo wrote:Now another question is it also possible to save/write string at compile time?Sort of, use pragma(msg, "some string"); and it will be printed out when that code is compiled. Important that it is when the code is compiled, NOT when the code is executed at compile time. So void foo() { pragma(msg, "here"); } will say "here" even if you never call foo. So you can't use it to print stuff out inside CTFE loops, and you might need to shield it with version {} or static if() {} to suppress printing. But you can use it to print a completed string or something and then redirect it to a file in your makefile to do something: string myfunction() { string value; foreach(a; ["a", "b"]) value ~= a; return value; } pragma(msg, myfunction()); // would say "ab" foo.txt If you find yourself wanting to print a lot, I say you should just use a regualr program that runs normally instead of CTFE. Since most the language works the same way, transitioning to and from regular execution and compile time execution is easy and generally doesn't need you to change most the code.
May 29 2014
Remo:is it also possible to save/write string at compile time?There is pragma(msg, "...") but it's a little crappy. There are plans and a pull request for a good _ctWrite, but it's stalled for reasons unknown to me. Bye, bearophile
May 29 2014
On Thursday, 29 May 2014 at 20:44:09 UTC, bearophile wrote:Remo:Thanks! Yes I know already about pragma(msg, "...") and use it all the time to debug. Something like _ctWrite would be great even if the name is not that great.is it also possible to save/write string at compile time?There is pragma(msg, "...") but it's a little crappy. There are plans and a pull request for a good _ctWrite, but it's stalled for reasons unknown to me. Bye, bearophile
May 29 2014