www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using file as input to stdin when compiling

reply Jamie <jamieborder01 gmail.com> writes:
Hi, I'm following through TDPL and am trying to import a txt file 
during compiling for the stdin.byLine() function to read. 
Currently I have


.
.
.
foreach (line; stdin.byLine()) {

}

and would like it to analyse the supplied text file. Is this 
possible in the way that I'm thinking, or is there another way?

Thanks
Feb 05 2018
next sibling parent Jamie <jamieborder01 gmail.com> writes:
On Tuesday, 6 February 2018 at 06:10:30 UTC, Jamie wrote:
 Hi, I'm following through TDPL and am trying to import a txt 
 file during compiling for the stdin.byLine() function to read. 
 Currently I have


 .
 .
 .
 foreach (line; stdin.byLine()) {

 }

 and would like it to analyse the supplied text file. Is this 
 possible in the way that I'm thinking, or is there another way?

 Thanks
I've figured out how to do it if I don't use the 1st line above, and manually compile and then execute: $ rdmd code.d < file.txt but still can't do it through the code file.
Feb 05 2018
prev sibling parent Seb <seb wilzba.ch> writes:
On Tuesday, 6 February 2018 at 06:10:30 UTC, Jamie wrote:
 Hi, I'm following through TDPL and am trying to import a txt 
 file during compiling for the stdin.byLine() function to read. 
 Currently I have


 .
 .
 .
 foreach (line; stdin.byLine()) {

 }

 and would like it to analyse the supplied text file. Is this 
 possible in the way that I'm thinking, or is there another way?

 Thanks

That's limitation of the shebang line. Most implementation's only accept one argument. https://en.wikipedia.org/wiki/Shebang_(Unix) However, you can simply do: ./foo.d < foo.text or: void main() { auto f = File(args[1]); } or rdmd --loop for tiny scripts:
  echo "abc" | rdmd --loop='line.splitter("").writeln
Feb 06 2018