www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - eof

reply "lx" <zdhtlixin 126.com> writes:
Hi,everyone,
I am learning D.I installed a plugin-visusl D to visual studio 
2008.I am very confused that ctrl+z didn't teminate the input of 
console,it result in a dead loop.When reading from a txt file,a 
extra line will be read and usually it result in a wrong 
result.Is there anyone who has experienced such a situation and 
can tell me what I should pay attention to.
Thanks a lot.
lx
Jun 24 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
lx:

 I am very confused that ctrl+z didn't teminate the input of 
 console,it result in a dead loop.
I think this is a library bug, I noticed it some times, but I didn't file it. Maybe it's worth filing in Bugzilla.
 When reading from a txt file,a extra line will be read and 
 usually it result in a wrong result.
Can you show the code? Bye, bearophile
Jun 24 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 I am very confused that ctrl+z didn't teminate the input of 
 console,it result in a dead loop.
I think this is a library bug, I noticed it some times, but I didn't file it. Maybe it's worth filing in Bugzilla.
I have added this bug report, is this the issue you are seeing/having? http://d.puremagic.com/issues/show_bug.cgi?id=10467 Bye, bearophile
Jun 24 2013
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 24 June 2013 at 21:13:31 UTC, bearophile wrote:
 I am very confused that ctrl+z didn't teminate the input of 
 console,it result in a dead loop.
I think this is a library bug, I noticed it some times, but I didn't file it. Maybe it's worth filing in Bugzilla.
I have added this bug report, is this the issue you are seeing/having? http://d.puremagic.com/issues/show_bug.cgi?id=10467 Bye, bearophile
I don't think this is a bug (I replied on the bug report): terminating the stream doesn't mean terminating the program, and if the program doesn't know how to handle a closed/eof/error'd stdin, it will just loop... This FAQ link explains it pretty well for C++, which is pretty much the same thing as in D: http://www.parashift.com/c++-faq/stream-input-failure.html (the next few points are relevant too). We could argue the design isn't optimal, yes, but it's not bugged.
Jun 24 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
monarch_dodra:

 I don't think this is a bug
I see. Then lx will have to explain the problem better.
 (I replied on the bug report):
I have given an answer, comparing to Python. I think the current Phobos behavour is bad. Bye, bearophile
Jun 24 2013
prev sibling parent reply "lx" <zdhtlixin 126.com> writes:
On Monday, 24 June 2013 at 21:13:31 UTC, bearophile wrote:
 I am very confused that ctrl+z didn't teminate the input of 
 console,it result in a dead loop.
I think this is a library bug, I noticed it some times, but I didn't file it. Maybe it's worth filing in Bugzilla.
I have added this bug report, is this the issue you are seeing/having? http://d.puremagic.com/issues/show_bug.cgi?id=10467 Bye, bearophile
I read those information and yes,it is the issue I am having.I should have handled it in right way.Ctrl+z seems close the stream.So,if I want to input another batch of data,it became impossilbe.So,how to reopen the stream again to allow me to input another batch of data? And,another problem:if I input ctrl+c,the code will terminate abnormally.Why this happened? Thanks. lx
Jun 25 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/25/2013 09:26 PM, lx wrote:

 Ctrl+z seems close the stream.So,if I want
 to input another batch of data,it became impossilbe.So,how to reopen the
 stream again to allow me to input another batch of data?
Making a copy of stdin works on Linux (where the stream is terminated by Ctrl-D in the console): import std.stdio; import std.algorithm; void main() { auto stdin_dup = stdin; stdin .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done"); stdin_dup .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done again"); } Ali
Jun 25 2013
parent reply "lx" <zdhtlixin 126.com> writes:
On Wednesday, 26 June 2013 at 04:46:32 UTC, Ali Çehreli wrote:
 On 06/25/2013 09:26 PM, lx wrote:

 Ctrl+z seems close the stream.So,if I want
 to input another batch of data,it became impossilbe.So,how to
reopen the
 stream again to allow me to input another batch of data?
Making a copy of stdin works on Linux (where the stream is terminated by Ctrl-D in the console): import std.stdio; import std.algorithm; void main() { auto stdin_dup = stdin; stdin .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done"); stdin_dup .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done again"); } Ali
Hi,Ali,I tried those codes,it doesn't work under win7.The result is "done again" will follow "done",without giving me any chance to input. lx
Jun 26 2013
parent "lx" <zdhtlixin 126.com> writes:
On Wednesday, 26 June 2013 at 13:45:41 UTC, lx wrote:
 On Wednesday, 26 June 2013 at 04:46:32 UTC, Ali Çehreli wrote:
 On 06/25/2013 09:26 PM, lx wrote:

 Ctrl+z seems close the stream.So,if I want
 to input another batch of data,it became impossilbe.So,how to
reopen the
 stream again to allow me to input another batch of data?
Making a copy of stdin works on Linux (where the stream is terminated by Ctrl-D in the console): import std.stdio; import std.algorithm; void main() { auto stdin_dup = stdin; stdin .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done"); stdin_dup .byLine(KeepTerminator.yes) .copy(stdout.lockingTextWriter); writeln("done again"); } Ali
Hi,Ali,I tried those codes,it doesn't work under win7.The result is "done again" will follow "done",without giving me any chance to input again after I enter ctrl-z . lx
Jun 26 2013
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/25/2013 09:26 PM, lx wrote:

 I input ctrl+c,the code will terminate
 abnormally.Why this happened?
Ctrl-C appears as SIGINT under POSIX systems. You need to register a handler for that signal: import std.stdio; import std.string; import core.sys.posix.signal; bool isDone = false; extern(C) void mySIGINThandler(int) nothrow system { isDone = true; } void main() { signal(SIGINT, &mySIGINThandler); while (true) { string line = chomp(readln()); if (stdin.eof() || isDone) { break; } else { writefln("Nice line: %s", line); } } } Ali
Jun 25 2013