Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d programming language : standard input problem or misunderstanding?

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! :)

like image 601
Redger Avatar asked Apr 07 '11 13:04

Redger


2 Answers

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.

like image 71
dsimcha Avatar answered Oct 01 '22 11:10

dsimcha


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

like image 41
he_the_great Avatar answered Oct 01 '22 09:10

he_the_great