Getting familiar with std.process

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 9 12:44:18 PST 2017


I haven't used std.process before and am trying to play around 
with it.

In the code below, the first file writes some text to an output 
file. My goal is to be able to read what is written to that file 
without creating the file itself. I'm not sure it's possible, but 
the second file is my attempt.

The second file compiles the first (for the sake of simplicity), 
then creates a pipe with program it compiles, piping the result 
to stdout, then it saves the result to an output string, which I 
then print (or potentially manipulate in some other way in the 
future). But it doesn't print anything (why I'm posting here).

I think the issue is that create_file doesn't write to stdout, it 
writes to file. Other than reading the file and then deleting it, 
I don't know what else to try.

//create_file.d

import std.stdio : toFile;

void main()
{
	toFile("test", "output.txt");
}

//read_file.d
//read_file.d

import std.process;
import std.stdio;

void main()
{
	auto command1 = execute(["dmd", "create_file.d"]);
	//auto command2 = execute(["create_file.exe"]);
	
	auto pipes = pipeProcess("create_file.exe", Redirect.stdout);
	scope(exit) wait(pipes.pid);
	
	foreach (line; pipes.stdout.byLine) writeln(line);
}


More information about the Digitalmars-d-learn mailing list