07 d using standard programming io flow redirection

//名字为a.d
import std.stdio;

void main() {
    double number;
    readf(" %s", &number);
    writeln(number * 2);
}//太奇怪,这么一个程序`300kb`?,看来d这确实`标准库和运行时`太大了.

Command a >1.txt, pay attention to enter 数字. Enter other words, will always enter down.
Similarly, 2.txtenter the number, then the command a <2.txt, the console shows the results came.
Can be used simultaneously a <2.txt >1.txt. Can be used |to connect them together.
This will be 上一个程序the output (conduit to) => 下一个程序is input.

file

was bom 字节序标记, bulk, small head, UTF8. 3 bytes for a specific utf16,32labeled bulk head.
with std.filethe Filestructure
when reading can be multithreaded / multi-program simultaneous read and write time is necessary to the.
File file = File("student_records", "r");
There are r,w,a,w+,a+other ways.
localized open the file area, closed automatically out of the area, other 栈对象/局部对象the same
as a different file is then opened

file.close();//先关闭
file.open("student_records","r");//再打开
//从文件读写
writeln("hello");
stdout.writeln("hello");//与上一样
file.writeln("hello");//写至特定文件

eofEnd of the file list, the file in this module , existsdetermines whether there is a file
the file open mode:
r, reading from the beginning.
r+: Read-write (re-write),
wthe file does not exist, an empty build file exists, i.e., is always empty.. 空文件.
w+, and uplink the same, but there are privileges.
aadditional, the file does not exist, build an empty file exists, preserve the contents, and ready to write the end of the file content
a+, and write on, like, read it from the beginning to read. reading can also writable.
can add b, expressed as a 二进制reading, but in the posixsystem, useless,

import std.stdio;

void main() {
    File file = File("student_records", "w");//模式
    file.writeln("Name  : ", "Zafer");
    file.writeln("Number: ", 123);
    file.writeln("Class : ", "1A");
}//readf,就像读流一样

Write to a file, and use stdoutthe same.
Because File needs a constant , if it is char[], you need idupto convert it, then.

import std.stdio;
import std.string;

void main() {
    File file = File("student_records", "r");
    //读文件,这种方式,其实不是很方便.

    while (!file.eof()) {
        string line = strip(file.readln());
        writeln("read line -> |", line);
    }
}

This auto var = VeryLongTypeName( / * ... * /);
fact is still a bit uncomfortable .c ++ so.
型 a{...};On the beginning of the end. Is not Gengshuang?

    auto duration = 42;
    auto distance = 1.2;
    auto greeting = "Hello";
    auto vehicle = BeautifulBicycle("blue");

These auto, in fact 占位符, derives its optimum type.
typeofSimilar c ++ is decltype(...)derived types.
In 模板与插件very useful in.

Published 381 original articles · won praise 25 · Views 100,000 +

Guess you like

Origin blog.csdn.net/fqbqrr/article/details/104587178