Jump to page: 1 2
Thread overview
basic interactive readf from stdin
Dec 26, 2015
Jay Norwood
Dec 26, 2015
Adam D. Ruppe
Dec 26, 2015
karthikeyan
Dec 26, 2015
Adam D. Ruppe
Dec 26, 2015
tcak
Dec 26, 2015
Jay Norwood
Dec 27, 2015
Jay Norwood
Dec 27, 2015
Ali Çehreli
Dec 27, 2015
Jay Norwood
Dec 27, 2015
Karthikeyan
Dec 27, 2015
Ali Çehreli
Dec 27, 2015
Karthikeyan
Dec 26, 2015
Jay Norwood
Dec 27, 2015
Ali Çehreli
December 26, 2015
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

December 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)
December 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:
>> 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
December 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.

December 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:
>> 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.
December 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:
>> 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.


December 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:
>> 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


December 27, 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:
>> 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;
}
December 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: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

December 26, 2015
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

« First   ‹ Prev
1 2