www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - basic interactive readf from stdin

reply Jay Norwood <jayn prismnet.com> writes:
Simple VS console app in D.  Reading lines to a string variable 
interactively. Object is to have no extra blank lines in the 
console output.  Seems very broken for this use, requiring two 
extra "enter" entries before the outputs both appear. Version 
DMD32 D Compiler v2.069.2

import std.stdio;

int main(string[] argv)
{
  string nm;
  stdin.readf("%s\n",&nm);
  writeln("nm:",nm);
  stdin.readf("%s\n",&nm);
  writeln("nm:",nm);
  return 0;
}

======== io shown below
123
456
nm:123

nm:456
Dec 26 2015
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 26 December 2015 at 19:40:59 UTC, Jay Norwood wrote:
 Simple VS console app in D.
If you are running inside visual studio, you need to be aware that output will be block buffered, not line buffered, because VS pipes the output making the program think it is talking to another program instead of to an interactive console (well, because it is!) Add a stdout.flush(); after writing to force it to show immediately. I really think the read functions ought to flush output too because this is such a FAQ. (indeed, my terminal.d does flush output when you request input)
Dec 26 2015
next sibling parent reply karthikeyan <tir.karthi gmail.com> writes:
On Saturday, 26 December 2015 at 19:52:15 UTC, Adam D. Ruppe 
wrote:
 On Saturday, 26 December 2015 at 19:40:59 UTC, Jay Norwood 
 wrote:
 Simple VS console app in D.
If you are running inside visual studio, you need to be aware that output will be block buffered, not line buffered, because VS pipes the output making the program think it is talking to another program instead of to an interactive console (well, because it is!) Add a stdout.flush(); after writing to force it to show immediately. I really think the read functions ought to flush output too because this is such a FAQ. (indeed, my terminal.d does flush output when you request input)
I experience the same as the OP on Linux Mint 15 with dmd2.069 and 64 bit machine. I have to press enter twice to get the output. I read http://ddili.org/ders/d.en/input.html and inserted a space before %s but still no use. Am I missing something here with the latest version? Code import std.stdio; int main(string[] argv) { string nm; readf(" %s\n",&nm); writeln("nm:",nm); // readf(" %s\n",&nm); // writeln("nm:",nm); return 0; } Output 56 2 nm:56
Dec 26 2015
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan wrote:
 I experience the same as the OP on Linux Mint 15 with dmd2.069 
 and 64 bit machine.  I have to press enter twice to get the 
 output. I read http://ddili.org/ders/d.en/input.html and 
 inserted a space before %s but still no use. Am I missing 
 something here with the latest version?
Oh, I'm sorry, it isn't buffering, it is readfing into a string here which is weird. Maybe try readln instead of readf.
Dec 26 2015
next sibling parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Saturday, 26 December 2015 at 20:19:08 UTC, Adam D. Ruppe 
wrote:
 On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan 
 wrote:
 I experience the same as the OP on Linux Mint 15 with dmd2.069 
 and 64 bit machine.  I have to press enter twice to get the 
 output. I read http://ddili.org/ders/d.en/input.html and 
 inserted a space before %s but still no use. Am I missing 
 something here with the latest version?
Oh, I'm sorry, it isn't buffering, it is readfing into a string here which is weird. Maybe try readln instead of readf.
As far as I remember, in C, if I was to be putting "\n" in scanf after %s, that double entering was happening. I guess that's the same problem. Trying same code without \n in readf can fix it I guess.
Dec 26 2015
parent Jay Norwood <jayn prismnet.com> writes:
On Saturday, 26 December 2015 at 20:38:52 UTC, tcak wrote:
 On Saturday, 26 December 2015 at 20:19:08 UTC, Adam D. Ruppe 
 wrote:
 On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan 
 wrote:
 I experience the same as the OP on Linux Mint 15 with 
 dmd2.069 and 64 bit machine.  I have to press enter twice to 
 get the output. I read http://ddili.org/ders/d.en/input.html 
 and inserted a space before %s but still no use. Am I missing 
 something here with the latest version?
Oh, I'm sorry, it isn't buffering, it is readfing into a string here which is weird. Maybe try readln instead of readf.
As far as I remember, in C, if I was to be putting "\n" in scanf after %s, that double entering was happening. I guess that's the same problem. Trying same code without \n in readf can fix it I guess.
import std.stdio; int main(string[] argv) { string nm; stdin.readf("%s",&nm); writeln("nm:",nm); stdout.flush(); stdin.readf("%s",&nm); writeln("nm:",nm); stdout.flush(); return 0; } ok, I tried above, adding both the stdout.flush() and removing the \n from the format. It didn't write to output even after a couple of enter's. When I entered ctrl-Z, it output below. ============ output running from command prompt 123 456 ^Z nm:123 456 nm:123 456
Dec 26 2015
prev sibling parent Jay Norwood <jayn prismnet.com> writes:
On Saturday, 26 December 2015 at 20:19:08 UTC, Adam D. Ruppe 
wrote:
 On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan 
 wrote:
 I experience the same as the OP on Linux Mint 15 with dmd2.069 
 and 64 bit machine.  I have to press enter twice to get the 
 output. I read http://ddili.org/ders/d.en/input.html and 
 inserted a space before %s but still no use. Am I missing 
 something here with the latest version?
Oh, I'm sorry, it isn't buffering, it is readfing into a string here which is weird. Maybe try readln instead of readf.
The use of readf into a string is demonstrated in a stdio.d unit test. I assumed it might also work with stdin. string s; auto f = File(deleteme); f.readf("%s\n", &s); assert(s == "hello", "["~s~"]"); f.readf("%s\n", &s); assert(s == "world", "["~s~"]"); ============= I did get this below to work with readln, although since readln didn't consume the terminator, I had to add the chomp() call. import std.stdio; import std.string; int main(string[] argv) { string nm, nm2; nm=readln('\n'); nm2 = nm.chomp(); writeln("nm:",nm2); nm=readln('\n'); nm2 = nm.chomp(); writeln("nm:",nm2); return 0; }
Dec 26 2015
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/26/2015 12:11 PM, karthikeyan wrote:

 I read http://ddili.org/ders/d.en/input.html and inserted a space 
before %s
 but still no use. Am I missing something here with the latest version?
The answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html Ali
Dec 26 2015
next sibling parent Jay Norwood <jayn prismnet.com> writes:
On Sunday, 27 December 2015 at 00:20:51 UTC, Ali Çehreli wrote:
 On 12/26/2015 12:11 PM, karthikeyan wrote:

 I read http://ddili.org/ders/d.en/input.html and inserted a
space before %s
 but still no use. Am I missing something here with the latest
version? The answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html Ali
Yes, thank you, strip() appears to be more useful than chomp() in this case.
Dec 26 2015
prev sibling parent reply Karthikeyan <tir.karthi gmail.com> writes:
On Sunday, 27 December 2015 at 00:20:51 UTC, Ali Çehreli wrote:
 On 12/26/2015 12:11 PM, karthikeyan wrote:

 I read http://ddili.org/ders/d.en/input.html and inserted a
space before %s
 but still no use. Am I missing something here with the latest
version? The answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html Ali
Many thanks Ali. The book says ctrl + D to end input. But I used two enters to get the output. Any idea why? The book was great. Thanks a lot.
Dec 26 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/26/2015 05:15 PM, Karthikeyan wrote:

 The answer is nine chapters later. :) (Use readln() and strip() (or
 chomp())).

   http://ddili.org/ders/d.en/strings.html

 Ali
Many thanks Ali. The book says ctrl + D to end input. But I used two enters to get the output. Any idea why?
I guess that means that my understanding was not portable. It requires Ctrl-D on my console environment on Linux. No matter how many Enters I enter :p they become parts of the same string. Ali
Dec 26 2015
parent Karthikeyan <tir.karthi gmail.com> writes:
On Sunday, 27 December 2015 at 02:08:05 UTC, Ali Çehreli wrote:
 On 12/26/2015 05:15 PM, Karthikeyan wrote:

 The answer is nine chapters later. :) (Use readln() and
strip() (or
 chomp())).

   http://ddili.org/ders/d.en/strings.html

 Ali
Many thanks Ali. The book says ctrl + D to end input. But I
used two
 enters to get the output. Any idea why?
I guess that means that my understanding was not portable. It requires Ctrl-D on my console environment on Linux. No matter how many Enters I enter :p they become parts of the same string. Ali
:) I was on zsh with gnome terminal alike on Linux Mint 15. Thanks for clearing that up.
Dec 26 2015
prev sibling parent Jay Norwood <jayn prismnet.com> writes:
On Saturday, 26 December 2015 at 19:52:15 UTC, Adam D. Ruppe 
wrote:
 On Saturday, 26 December 2015 at 19:40:59 UTC, Jay Norwood 
 wrote:
 Simple VS console app in D.
If you are running inside visual studio, you need to be aware that output will be block buffered, not line buffered, because VS pipes the output making the program think it is talking to another program instead of to an interactive console (well, because it is!) Add a stdout.flush(); after writing to force it to show immediately. I really think the read functions ought to flush output too because this is such a FAQ. (indeed, my terminal.d does flush output when you request input)
It doesn't make a difference if I run in VS or from a console window. I had also already tried various forms stdout.flush(). It doesn't make a difference ... still requires two extra enters before it outputs the data. I haven't tried it in linux yet.
Dec 26 2015
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/26/2015 11:40 AM, Jay Norwood wrote:
 Simple VS console app in D.  Reading lines to a string variable
 interactively. Object is to have no extra blank lines in the console
 output.  Seems very broken for this use, requiring two extra "enter"
 entries before the outputs both appear. Version DMD32 D Compiler v2.069.2

 import std.stdio;

 int main(string[] argv)
 {
   string nm;
   stdin.readf("%s\n",&nm);
   writeln("nm:",nm);
   stdin.readf("%s\n",&nm);
   writeln("nm:",nm);
   return 0;
 }

 ======== io shown below
 123
 456
 nm:123

 nm:456
Reading lines with readln works in a Linux console: import std.stdio; import std.string; int main(string[] argv) { string nm; nm = readln.strip; writeln("nm:",nm); nm = readln.strip; writeln("nm:",nm); return 0; } Ali
Dec 26 2015