digitalmars.D.learn - basic interactive readf from stdin
- Jay Norwood (20/20) Dec 26 2015 Simple VS console app in D. Reading lines to a string variable
- Adam D. Ruppe (10/11) Dec 26 2015 If you are running inside visual studio, you need to be aware
- karthikeyan (22/34) Dec 26 2015 I experience the same as the OP on Linux Mint 15 with dmd2.069
- Adam D. Ruppe (3/8) Dec 26 2015 Oh, I'm sorry, it isn't buffering, it is readfing into a string
- tcak (6/15) Dec 26 2015 As far as I remember, in C, if I was to be putting "\n" in scanf
- Jay Norwood (24/40) Dec 26 2015 import std.stdio;
- Jay Norwood (26/35) Dec 26 2015 The use of readf into a string is demonstrated in a stdio.d unit
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/8) Dec 26 2015 The answer is nine chapters later. :) (Use readln() and strip() (or
- Jay Norwood (3/12) Dec 26 2015 Yes, thank you, strip() appears to be more useful than chomp() in
- Karthikeyan (4/13) Dec 26 2015 Many thanks Ali. The book says ctrl + D to end input. But I used
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/13) Dec 26 2015 I guess that means that my understanding was not portable. It requires
- Karthikeyan (3/19) Dec 26 2015 :) I was on zsh with gnome terminal alike on Linux Mint 15.
- Jay Norwood (6/18) Dec 26 2015 It doesn't make a difference if I run in VS or from a console
- =?UTF-8?Q?Ali_=c3=87ehreli?= (14/33) Dec 26 2015 Reading lines with readln works in a Linux console:
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
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
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: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:56Simple 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
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
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: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.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
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: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 456On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan wrote: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.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
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: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; }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
On 12/26/2015 12:11 PM, karthikeyan wrote:I read http://ddili.org/ders/d.en/input.html and inserted a spacebefore %sbut 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
On Sunday, 27 December 2015 at 00:20:51 UTC, Ali Çehreli wrote:On 12/26/2015 12:11 PM, karthikeyan wrote:Yes, thank you, strip() appears to be more useful than chomp() in this case.I read http://ddili.org/ders/d.en/input.html and inserted aspace before %sbut still no use. Am I missing something here with the latestversion? The answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html Ali
Dec 26 2015
On Sunday, 27 December 2015 at 00:20:51 UTC, Ali Çehreli wrote:On 12/26/2015 12:11 PM, karthikeyan wrote: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.I read http://ddili.org/ders/d.en/input.html and inserted aspace before %sbut still no use. Am I missing something here with the latestversion? The answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html Ali
Dec 26 2015
On 12/26/2015 05:15 PM, Karthikeyan wrote: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. AliThe answer is nine chapters later. :) (Use readln() and strip() (or chomp())). http://ddili.org/ders/d.en/strings.html AliMany thanks Ali. The book says ctrl + D to end input. But I used two enters to get the output. Any idea why?
Dec 26 2015
On Sunday, 27 December 2015 at 02:08:05 UTC, Ali Çehreli wrote:On 12/26/2015 05:15 PM, Karthikeyan wrote::) I was on zsh with gnome terminal alike on Linux Mint 15. Thanks for clearing that up.strip() (orThe answer is nine chapters later. :) (Use readln() andused twochomp())). http://ddili.org/ders/d.en/strings.html AliMany thanks Ali. The book says ctrl + D to end input. But Ienters 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
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: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.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
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:456Reading 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