www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Threads

reply vino <vino_ss hotmail.com> writes:
Hi All,

  I am a newbie for D programming and need some help, I am trying 
to write a program using the example given in the book The "D 
Programming Language" written by "Andrei Alexandrescu" with few 
changes such as the example program read the input from stdin and 
prints the data to stdout, but my program reads the input from 
the file(readfile.txt) and writes the output to another 
file(writefile.txt), and I am getting the below errors while 
compiling

Error:


readwriteb.d(7): Error: cannot implicitly convert expression 
(__aggr2859.front()) of type ubyte[] to immutable(ubyte)[]
readwriteb.d(15): Error: cannot implicitly convert expression 
(receiveOnly()) of type immutable(ubyte)[] to 
std.outbuffer.OutBuffer


Version: DMD64 D Compiler v2.071.0

Code:

import std.algorithm, std.concurrency, std.stdio, std.outbuffer, 
std.file;

void main() {
    enum bufferSize = 1024 * 100;
    auto file = File("readfile.txt", "r");
    auto tid = spawn(&fileWriter);
    foreach (immutable(ubyte)[] buffer; file.byChunk(bufferSize)) {
       send(tid, buffer);
    }
}

void fileWriter() {
    auto wbuf  = new OutBuffer();
    for (;;) {
       wbuf = receiveOnly!(immutable(ubyte)[])();
       write("writefile.txt", wbuf);
    }
}

From,
Vino.B
May 02 2016
next sibling parent Alex Parrill <initrd.gz gmail.com> writes:
On Monday, 2 May 2016 at 16:39:13 UTC, vino wrote:
 Hi All,

  I am a newbie for D programming and need some help, I am 
 trying to write a program using the example given in the book 
 The "D Programming Language" written by "Andrei Alexandrescu" 
 with few changes such as the example program read the input 
 from stdin and prints the data to stdout, but my program reads 
 the input from the file(readfile.txt) and writes the output to 
 another file(writefile.txt), and I am getting the below errors 
 while compiling

 Error:


 readwriteb.d(7): Error: cannot implicitly convert expression 
 (__aggr2859.front()) of type ubyte[] to immutable(ubyte)[]
 readwriteb.d(15): Error: cannot implicitly convert expression 
 (receiveOnly()) of type immutable(ubyte)[] to 
 std.outbuffer.OutBuffer


 Version: DMD64 D Compiler v2.071.0

 Code:

 import std.algorithm, std.concurrency, std.stdio, 
 std.outbuffer, std.file;

 void main() {
    enum bufferSize = 1024 * 100;
    auto file = File("readfile.txt", "r");
    auto tid = spawn(&fileWriter);
    foreach (immutable(ubyte)[] buffer; 
 file.byChunk(bufferSize)) {
       send(tid, buffer);
    }
 }

 void fileWriter() {
    auto wbuf  = new OutBuffer();
    for (;;) {
       wbuf = receiveOnly!(immutable(ubyte)[])();
       write("writefile.txt", wbuf);
    }
 }

 From,
 Vino.B
File.byChunks iirc returns a mutable ubyte[] range, not an immutable(ubyte)[]. The easiest way to fix this would be to change the foreach variable to ubyte[] and make an immutable duplicate of it when sending via idup. wbuf is inferred to be an OutBuffer but then you assign an immutable(ubyte)[] to it in your foreach loop; a type error.
May 02 2016
prev sibling parent safety0ff <safety0ff.dev gmail.com> writes:
On Monday, 2 May 2016 at 16:39:13 UTC, vino wrote:
 Hi All,

  I am a newbie for D programming and need some help, I am 
 trying to write a program using the example given in the book 
 The "D Programming Language" written by "Andrei Alexandrescu"
Make sure to check the errata on his site: http://erdani.com/index.php?cID=109
 Page: 406 Print: 1
 Current text:
     foreach (immutable(ubyte)[] buffer; 
 stdin.byChunk(bufferSize)) send(tid, buffer);
 Correction:
     foreach (buffer; stdin.byChunk(bufferSize)) send(tid, 
 buffer.idup)
May 02 2016