www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reading console in an 'endless' loop

reply "Alexander Panek" <alexander.panek brainsware.org> writes:
Hello,

I`m programming a little shell in D with some scripting-possibilities.  
Reading a file is no problem, using std.stream.file. But I also have to  
read stdin to get some input from the user - that`s where my program  
'crashes' in any way.

First I`ve tried to do it like that:

while(true)
     write(prompt); // writing something like "10:20:33$c:\>"
     scanf("%*.s", input); // input is char[]
}

In the very first step of the loop all works fine, but as soon as i hit  
enter/return it just prints prompt, without waiting for an acknowledge  
 from stdin. I also tried it with getc(), getch(), getchar(), .. - always  
printing just my prompt-string after the first step.

Is there anybody who already tried something like that? I don`t really see  
a problem there, it`s the same way as I`d do it in C (as far as we have  
only C-wrappers for stdin).

Regards,
Alex

-- 
huh? did you say something? :o
Jun 12 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 12 Jun 2005 11:28:15 +0200, Alexander Panek wrote:

 Hello,
 
 I`m programming a little shell in D with some scripting-possibilities.  
 Reading a file is no problem, using std.stream.file. But I also have to  
 read stdin to get some input from the user - that`s where my program  
 'crashes' in any way.
 
 First I`ve tried to do it like that:
 
 while(true)
      write(prompt); // writing something like "10:20:33$c:\>"
      scanf("%*.s", input); // input is char[]
 }
 
 In the very first step of the loop all works fine, but as soon as i hit  
 enter/return it just prints prompt, without waiting for an acknowledge  
  from stdin. I also tried it with getc(), getch(), getchar(), .. - always  
 printing just my prompt-string after the first step.
 
 Is there anybody who already tried something like that? I don`t really see  
 a problem there, it`s the same way as I`d do it in C (as far as we have  
 only C-wrappers for stdin).
 
 Regards,
 Alex

The code below works ... <code> module test; import std.cstream; import std.stream; void main() { char[] prompt; char[] input; prompt = "Yeah?: "; while(input != "quit" && din.eof() == false) { dout.writef("%s", prompt); input = din.readLine(); } } </code> -- Derek Parnell Melbourne, Australia 12/06/2005 9:31:22 PM
Jun 12 2005
next sibling parent "Alexander Panek" <alexander.panek brainsware.org> writes:
On Sun, 12 Jun 2005 13:32:49 +0200, Derek Parnell <derek psych.ward> wrote:

 On Sun, 12 Jun 2005 11:28:15 +0200, Alexander Panek wrote:

 Hello,

 I`m programming a little shell in D with some scripting-possibilities.
 Reading a file is no problem, using std.stream.file. But I also have to
 read stdin to get some input from the user - that`s where my program
 'crashes' in any way.

 First I`ve tried to do it like that:

 while(true)
      write(prompt); // writing something like "10:20:33$c:\>"
      scanf("%*.s", input); // input is char[]
 }

 In the very first step of the loop all works fine, but as soon as i hit
 enter/return it just prints prompt, without waiting for an acknowledge
  from stdin. I also tried it with getc(), getch(), getchar(), .. -  
 always
 printing just my prompt-string after the first step.

 Is there anybody who already tried something like that? I don`t really  
 see
 a problem there, it`s the same way as I`d do it in C (as far as we have
 only C-wrappers for stdin).

 Regards,
 Alex

The code below works ... <code> module test; import std.cstream; import std.stream; void main() { char[] prompt; char[] input; prompt = "Yeah?: "; while(input != "quit" && din.eof() == false) { dout.writef("%s", prompt); input = din.readLine(); } } </code>

thanks, works fine! :) -- huh? did you say something? :o
Jun 12 2005
prev sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
 The code below works ...
 <code>
 module test;
 import std.cstream;
 import std.stream;

note std.cstream publically imports both std.stream and std.c.stdio so the second import isn't needed.
Jun 12 2005