Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact
Version 5 (modified by erpe, 14 years ago)
0.99.9 - compat

This document has been placed into the public domain by Kris

/*******************************************************************************

       Create a file for random access. Write some stuff to it, rewind to
       file start and read back.

*******************************************************************************/

private import  tango.io.device.File,
                tango.io.stream.Data;

void main()
{
        // open a file for reading
        auto fc = new FileConduit ("random.bin", FileConduit.ReadWriteCreate);
        
        scope (exit)
               fc.close;

        // construct (binary) reader & writer upon this conduit
        auto output = new DataOutput(fc);
        auto input = new DataInput(fc);

        int x=10, y=20;

        // write some data and flush output since IO is buffered
        output.int32(x);
        output.int32(y);
        output.flush;

        // rewind to file start
        fc.seek (0);

        // read data back again, but swap destinations
        y = input.int32;
        x = input.int32;

        assert (y==10);
        assert (x==20);

        fc.close();
}