Why is there no std.stream anymore?

codephantom me at noyb.com
Tue Dec 12 03:03:59 UTC 2017


On Tuesday, 12 December 2017 at 02:15:13 UTC, codephantom wrote:
>
> just playing around with this....
>

also...in case you only want to read n bytes..

// -----------------------

module test;

import std.stdio, std.file, std.exception;
import std.datetime.stopwatch;


void main()
{
     string filename = "test.txt";  // a text file
     //string filename = "test.exe"; // a binary file

     enforce(filename.exists, "Umm..that file does not exist!");

     auto file = File(filename, "r");
     ubyte[] buf;

     import std.datetime : MonoTime;
     auto t2 = MonoTime.currTime;

     // just read the first n bytes.
     int bytesToRead = 4; // change this n
     int bufCount;
     while ( !file.eof() && bufCount < bytesToRead )
     {
         buf = file.rawRead(new ubyte[1]);

         if(!file.eof())
         {
             process(cast(char)(buf[0]));
             bufCount++;
         }
     }


     writeln("-------------------------------------");
     writeln("this took : ", MonoTime.currTime - t2);
     writeln("-------------------------------------");
     writeln();

     return;
}

void process(char someChar)
{
     import std.ascii : isPrintable;

     if( isPrintable(someChar) )
         writeln("found a printable character: ", someChar);
     else
         writeln("found a non printable character");

}
// -----------------------



More information about the Digitalmars-d-learn mailing list