Ctrl-Z in windows and byLine()

Jay Norwood jayn at prismnet.com
Wed Oct 12 13:33:32 PDT 2011


The error came in the case of entering ctrl-z on the first line.

The error msg on WinXP 32 was
^Z
object.Exception at D:\dmd\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(943):
Enforcement failed
----------------
D:\dmd\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(944): std


A solution that avoids the error is to add a check for stdin.eof().

void dict1_4()
{
        size_t[string] dict; // see errata use size_t
        foreach (line; stdin.byLine()){
                if (stdin.eof()) break;
                foreach (word; splitter(strip(line))){ // see errata required std.algorithm
                        if (word in dict) continue;
                        auto newID = dict.length;
                        dict[word.idup] = newID; // see errata required idup
                        writeln(newID, '\t', word);
                }
        }

}


Andrei Alexandrescu Wrote:

> Got this from a reader:
> 
> =======================
> I'm testing on Windows the code below, based on your errata changes for
> p8, print 1.  It works ok if on console I enter some characters, but if I
> terminate console input with ctrl-z, then there is an error exit of the
> program.  So, does this code need some exception handler to handle the
> immediate end of input from console?
> 
> void dict1_4()
> {
> 	size_t[string] dict; // see errata use size_t
> 	foreach (line; stdin.byLine()){
> 		foreach (word; splitter(strip(line))){ // see errata required 
> std.algorithm
> 			if (word in dict) continue;
> 			auto newID = dict.length;
> 			dict[word.idup] = newID; // see errata required idup
> 			writeln(newID, '\t', word);
> 		}
> 	}
> 
> }
> =======================
> 
> I thought Ctrl-Z simply sends EOF to the reader, so this should work. 
> What is byLine() doing wrong?
> 
> 
> Thanks,
> 
> Andrei



More information about the Digitalmars-d mailing list