reading in text files

Cristi Cobzarenco cristi.cobzarenco at gmail.com
Wed Aug 24 07:30:04 PDT 2011


The program reads the "file" from stdin, so you need to redirect stdin
to the file you want: try "./readingHamlet < hamlet.txt" or "cat
hamlet.txt | ./readingHamlet" without the quotes on a *NIX system.

---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco



On 24 August 2011 17:01, Brian Brady <brian.brady1982 at gmail.com> wrote:
> All
>
> I am working through Andrei Alexandrescus "The D Programming Language" but
> have hit a road block fairly early on.
>
> There is a program in the book which is designed to read through a text file
> and do a simple word count. The program looks like this:
>
> import std.stdio, std.string;
>
> void main()
> {
>  //Compute counts
>  uint[string] freqs;
>  foreach(line; stdin.byLine())
>  {
>    foreach(word; split(strip(line)))
>    {
>      ++freqs[word.idup];
>    }
>  }
>
>  //Prints count
>  foreach(key, value; freqs)
>  {
>    writefln("%6u\t%s", value, key);
>  }
> }
>
> My query is basically how to read the text file in?
>
> currently I am trying to use
> ./readingHamlet cat hamlet.txt
>
> but it just hangs there, not doing anything(for a considerable time) so I am
> assuming I am doing something wrong. There isn't any actual mention in the
> book of *how* reading in the text file should be accomplished, so what is the
> best way to do this?
>
> std.file?
>
> Seems silly providing a program that analyses a text file, without telling the
> reader how to read in the text file, so I am wondering if there is some
> assumed knowledge I am missing?
>
> Regards.
>


More information about the Digitalmars-d-learn mailing list