关于dlang语言的stdio模块以下内容请用好看的markdown格式翻译成中文内容如下:Iterates through the lines of a file by using foreachExamplevoid main foreach string line; linesstdin use line The line terminator n by default i
通过foreach遍历文件的每一行。
示例:
void main() { foreach (string line; lines(stdin)) { ... 使用line ... } }
行终止符(默认为'\n')是读取的字符串的一部分(文件的最后一行可能缺少它)。有几种类型支持line,lines的行为会相应地发生变化:
如果line的类型是string、wstring或dstring,则每次读取时都会分配一个新的相应类型的字符串。
如果line的类型是char[]、wchar[]或dchar[],则该行的内容将在读取中被重用(覆盖)。
如果line的类型是immutable(ubyte)[],则行为类似于情况(1),不过在输入时不会尝试进行UTF检查。
如果line的类型是ubyte[],则行为类似于情况(2),不过在输入时不会尝试进行UTF检查。
在所有情况下,还接受两个符号版本,其中第一个符号(整数类型,例如ulong或uint)跟踪当前行的从零开始的编号。
示例:
foreach (ulong i, string line; lines(stdin)) { ... 使用line ... }
如果出现I/O错误,则会抛出StdioException异常。
另见:byLine、this(File f, dchar terminator = '\n')。
构造函数。
参数:
File f:要从中读取行的文件。
dchar terminator:行分隔符(默认为'\n')。
auto chunks(File f, size_t size);
通过foreach逐块遍历文件。
示例:
void main() { foreach (ubyte[] buffer; chunks(stdin, 4096)) { ... 使用buffer ... } }
缓冲区的内容在调用之间重复使用。在上面的示例中,对于所有迭代,buffer.length均为4096,除了最后一个迭代,此时buffer.length可能小于4096(但始终大于零)。
如果出现I/O错误,则会抛出StdioException异常。
void toFile(T)(T data, string fileName) if (is(typeof(copy(data, stdout.lockingBinaryWriter))));
将数组或范围写入文件。简写为data.copy(File(fileName, "wb").lockingBinaryWriter)。类似于std.file.write,字符串是按原样写入的,而不是根据文件的方向编码。
跳转到:errno、opCall、this。
class StdioException: object.Exception;
如果出现I/O错误,则会抛出。
uint errno;
操作系统错误代码。
@trusted this(string message, uint e = core.stdc.errno.errno);
使用消息和错误代码初始化。
static @safe void opCall(string msg);
static @safe void opCall();
方便函数,抛出StdioException异常。
alias stdin = makeGlobal!"core.stdc.stdio.stdin".makeGlobal;
标准输入流。
返回值:
stdin作为File。
注意:
返回的File包装了core.stdc.stdio.stdin,因此是线程全局的。重新分配stdin到不同的File必须在单线程或锁定上下文中进行,以避免竞争条件。
所有从stdin读取的操作都会自动全局锁定文件,并导致所有其他调用read的线程等待,直到锁定被释放。
示例:
// 从stdin读取,对行进行排序,写入stdout import std.algorithm.mutation : copy; import std.algorithm.sorting : sort; import std.array : array; import std.typecons : Yes;
void main() { stdin // 从stdin读取 .byLineCopy(Yes.keepTerminator) // 复制每行 .array() // 转换为行数组 .sort() // 对行排序 .copy( // 复制.sort的输出到OutputRange stdout.lockingTextWriter()); // OutputRange }
alias stdout = makeGlobal!"core.stdc.stdio.stdout".makeGlobal;
标准输出流。
返回值:
stdout作为File。
注意:
返回的File包装了core.stdc.stdio.stdout,因此是线程全局的。重新分配stdout到不同的File必须在单线程或锁定上下文中进行,以避免竞争条件。
所有写入stdout的操作都会自动全局锁定文件,并导致所有其他调用write的线程等待,直到锁定被释放。
示例:
void main() { stdout.writeln("向stdout写入消息。"); }
示例:
void main() { import std.algorithm.iteration : filter, map, sum; import std.format : format; import std.range : iota, tee;
int len;
const r = 6.iota
.filter!(a => a % 2) // 1 3 5
.map!(a => a * 2) // 2 6 10
.tee!(_ => stdout.writefln("len: %d", len++))
.sum;
writeln(r); // 18
}
示例:
void main() { import std.algorithm.mutation : copy; import std.algorithm.iteration : map; import std.format : format; import std.range : iota;
10.iota
.map!(e => "N: %d".format(e))
.copy(stdout.lockingTextWriter()); // OutputRange
}
alias stderr = makeGlobal!"core.stdc.stdio.stderr".makeGlobal;
标准错误流。
返回值:
stderr作为File。
注意:
返回的File包装了core.stdc.stdio.stderr,因此是线程全局的。重新分配stderr到不同的File必须在单线程或锁定上下文中进行,以避免竞争条件。
所有写入stderr的操作都会自动全局锁定文件,并导致所有其他调用write的线程等待,直到锁定被释放。
示例:
void main() { stderr.writeln("向stderr写入消息。"); }
File openNetwork(string host, ushort port);
实验性的网络访问,通过文件接口。
打开到给定主机和端口的TCP连接,然后通过与任何其他文件相同的接口获得读取和写入访问(这意味着writef和byLine范围可以工作!)。
作者:
Adam D. Ruppe
错误:
仅在Linux上工作
原文地址: http://www.cveoy.top/t/topic/gSN1 著作权归作者所有。请勿转载和采集!