File: test_write.d

package info (click to toggle)
ironseed 0.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,816 kB
  • sloc: pascal: 27,286; ansic: 795; makefile: 282; perl: 277; sh: 229
file content (58 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import std.stdio;
import std.conv;

template PString(int L){
        align(1) struct ps {
                ubyte length;
                char[L] data;
                const int maxlength = L;
                void opCall(char []s) {
                        assert (maxlength < 255);
                        if(s.length > maxlength) {
                                this.length = to!ubyte(maxlength);
                                data[0..this.length] = s[0..this.length];
                        } else {
                                this.length = to!ubyte(s.length);
                                data[0..s.length] = s[0..s.length];
                        }
                }
                char []opCast() {
                        return data[0..this.length];
                }
        }
}

struct TitleRecord {
        short id;
        char[49] text;
        //PString!(49).ps text;
};

TitleRecord tr;

void main()
{


auto stream = File("filename","wb+");
 tr.id = 1234;
 tr.text = "hello world";
 //tr.text ( cast(char[])  "hello world!" );
 //ubyte[] outstring = cast(ubyte[]) "blabla";

 writeln(tr);
 //stream.write(tr);
 //stream.rawWrite(tr);
 stream.rawWrite((&tr)[0..1]);  // see https://stackoverflow.com/a/63489442/2600099
 //stream.rawWrite([tr]);
 //stream.rawWrite(cast(ubyte[52]) tr);
 //stream.rawWrite(cast(ubyte[]) tr);
 //fwrite(&tr, 4, 1, stream);

 //stream.rewind();
 //auto inbytes = new char[4];
 //stream.rawRead(inbytes);
 //writeln("inbytes=",inbytes);
 //assert(inbytes[3] == outstring[3]);
}