d programming language : standard input problem or misunderstanding?
Asked Answered
B

2

8

Here is a simple program that reads lines from stdin and outputs them to stdout.

module test;

import std.stdio; 

void main(string[] args) 
{

    foreach (int i, string line; lines(stdin)) {
        writeln(line ~ " (test)");
    }
}

I'm using the Windows DMD compiler v2.052.

If I do : type file.txt | test.exe

The program appends the word "test" to each line of file.txt and outputs them to the console.

However I keep getting an error at the end:

std.stdio.StdioException@std\stdio.d(2138): Bad file descriptor

Maybe I'm missing something? It drives me crazy! :)

Bosom answered 7/4, 2011 at 13:41 Comment(2)
I'm not familiar with the type command, maybe it isn't sending EOF when the file is done. In Linux you just do: ./test < file.txtFourgon
Wow. The problem is solved if I use this : test.exe < file.txt Turn your comment into an answer and I'll mark it as a solution: thanks!Bosom
F
4

I'm not familiar with the type command, maybe it isn't sending EOF when the file is done. In Linux you just do: ./test < file.txt

This is input redirection. Unlike piping, which turns the program output into standard input, this turns the file into standard input of the program. There is also output redirection which takes the output of the program and stores it in a file.

./test > output.txt

Fourgon answered 7/4, 2011 at 15:26 Comment(0)
P
6

This is a longstanding bug: http://d.puremagic.com/issues/show_bug.cgi?id=3425

What you're trying to do definitely works on non-Windows operating systems and should work on Windows, too. I think it's a bug in the Digital Mars implementation of the C I/O functions, which are being wrapped by std.stdio. I've tried to fix this bug before, but never even succeeded in identifying its root cause.

Potbellied answered 7/4, 2011 at 18:0 Comment(0)
F
4

I'm not familiar with the type command, maybe it isn't sending EOF when the file is done. In Linux you just do: ./test < file.txt

This is input redirection. Unlike piping, which turns the program output into standard input, this turns the file into standard input of the program. There is also output redirection which takes the output of the program and stores it in a file.

./test > output.txt

Fourgon answered 7/4, 2011 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.