www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problems with MemoryStream

reply Tobias Pankrath <tobias pankrath.net> writes:
Hello,

I just got my hands on a copy of TDPL and I like the book and D is very
promising. But I run into problems with the easiest of program. I just
want to write to and from a stream, just the way it is possible with
C++ stringstreams. But this program produces an empty line as 
output.

/** begin program
import std.stdio;
import std.stream;

void main() {
  auto stream = new MemoryStream( );
  string s = "this is a line";
 
  stream.writeLine(s);
  char[] get = stream.readLine();
  writeln(get);
}
 
// end of program

Why? Whats the right way to do it?

Greetings, Tobias
 
Jan 22 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 22 January 2011 14:32:18 Tobias Pankrath wrote:
 Hello,
 
 I just got my hands on a copy of TDPL and I like the book and D is very
 promising. But I run into problems with the easiest of program. I just
 want to write to and from a stream, just the way it is possible with
 C++ stringstreams. But this program produces an empty line as
 output.
 
 /** begin program
 import std.stdio;
 import std.stream;
 
 void main() {
   auto stream = new MemoryStream( );
   string s = "this is a line";
 
   stream.writeLine(s);
   char[] get = stream.readLine();
   writeln(get);
 }
 
 // end of program
 
 Why? Whats the right way to do it?

std.stream is going to be completely replaced with a properly range-based, streaming solution. Work has been done on an API for its replacement, and the API was reveiwed on the D list fairly recently, but I don't know exactly what it's currrent status is. And once the API has been agreed upon, then the code has to be written. So, it'll likely be a few releases before std.stream gets replaced. So, in the long run, you're not going to want to be using std.stream (or at least the current std.stream - I don't know if the replacement will be a new module or if it's just that the functions currently there will be deprecated and replaced with the new ones). Now, that doesn't stop you from using it now if you want to. But personally, I only ever use std.stdio and std.file, so I'm not well versed in how to use std.stream. I just thought that I should point out that it's not sticking around in its current form long term. - Jonathan M Davis
Jan 23 2011
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Tobias Pankrath wrote:
 Hello,
 
 I just got my hands on a copy of TDPL and I like the book and D is very
 promising. But I run into problems with the easiest of program. I just
 want to write to and from a stream, just the way it is possible with
 C++ stringstreams. But this program produces an empty line as 
 output.
 
 /** begin program
 import std.stdio;
 import std.stream;
 
 void main() {
   auto stream = new MemoryStream( );
   string s = "this is a line";
  
   stream.writeLine(s);

It appears that there is either a single read+write position, or writing also advances the read position. Add the following line here: stream.seek(0, SeekPos.Set);
   char[] get = stream.readLine();
   writeln(get);
 }
  
 // end of program
 
 Why? Whats the right way to do it?
 
 Greetings, Tobias
  

Ali
Jan 24 2011