d 编程语言:标准输入问题还是误解?
这是一个简单的程序,它从 stdin 读取行并将其输出到 stdout。
module test;
import std.stdio;
void main(string[] args)
{
foreach (int i, string line; lines(stdin)) {
writeln(line ~ " (test)");
}
}
我正在使用 Windows DMD 编译器 v2.052。
如果我这样做: 输入 file.txt | test.exe
该程序将单词“test”附加到file.txt的每一行并将其输出到控制台。
但是我最后总是收到错误:
std.stdio.StdioException@std\stdio.d(2138): Bad file detector
也许我错过了一些东西? 这让我发疯! :)
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! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个长期存在的错误:http://d.puremagic.com/issues/show_bug .cgi?id=3425
您尝试执行的操作肯定适用于非 Windows 操作系统,并且应该也适用于 Windows。我认为这是 CI/O 函数的 Digital Mars 实现中的一个错误,该函数由
std.stdio
包装。我以前曾尝试修复此错误,但从未成功地找出其根本原因。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.我不熟悉 type 命令,也许文件完成后它没有发送 EOF。在 Linux 中你只需执行: ./test < file.txt
这是输入重定向。与将程序输出转换为标准输入的管道不同,这将文件转换为程序的标准输入。还有输出重定向,它获取程序的输出并将其存储在文件中。
./测试>输出.txt
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