www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reading in text files

reply Brian Brady <brian.brady1982 gmail.com> writes:
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.
Aug 24 2011
next sibling parent reply Johannes Pfau <spam example.com> writes:
Brian Brady 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.

Hi, stdin.byLine() reads from the standard input, which is your console/keyboard input by default. The default stdin doesn't have an end, and unless you type something in, there's no input at all. That's why the program just hangs. On Linux/unix you can for example pipe the output from one command to another: cat hamlet.txt | ./readingHamlet this way readingHamlet's standard input is connected to cat's standard output. -- Johannes Pfau
Aug 24 2011
next sibling parent Brian Brady <brian.brady1982 gmail.com> writes:
== Quote from Johannes Pfau (spam example.com)'s article
 Brian Brady 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.

stdin.byLine() reads from the standard input, which is your console/keyboard input by default. The default stdin doesn't have an end, and unless you type something in, there's no input at all. That's why the program just hangs. On Linux/unix you can for example pipe the output from one command to another: cat hamlet.txt | ./readingHamlet this way readingHamlet's standard input is connected to cat's standard output.

This worked!! As I assumed, it was something simple :S Thank you so much.
Aug 24 2011
prev sibling parent travert phare.normalesup.org (Christophe) writes:
 The default stdin doesn't have an end, and unless you type something 
 in, there's no input at all. That's why the program just hangs.

You can end keyboard stdin by typing ^D (Ctrl + D) under unix.
Aug 24 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 24 Aug 2011 10:25:18 -0400, Johannes Pfau <spam example.com> wrote:

 On Linux/unix you can for example pipe the output from one command to
 another:

 cat hamlet.txt | ./readingHamlet

 this way readingHamlet's standard input is connected to cat's standard
 output.

I believe in all OSes (Windows included) ./readingHamlet < hamlet.txt works. Could be wrong though. -Steve
Aug 24 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Maybe ./readingHamlet < hamlet.txt
Aug 24 2011
prev sibling next sibling parent Cristi Cobzarenco <cristi.cobzarenco gmail.com> writes:
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 gmail.com> wrote:
 All

 I am working through Andrei Alexandrescus "The D Programming Language" bu=

 have hit a road block fairly early on.

 There is a program in the book which is designed to read through a text f=

 and do a simple word count. The program looks like this:

 import std.stdio, std.string;

 void main()
 {
 =A0//Compute counts
 =A0uint[string] freqs;
 =A0foreach(line; stdin.byLine())
 =A0{
 =A0 =A0foreach(word; split(strip(line)))
 =A0 =A0{
 =A0 =A0 =A0++freqs[word.idup];
 =A0 =A0}
 =A0}

 =A0//Prints count
 =A0foreach(key, value; freqs)
 =A0{
 =A0 =A0writefln("%6u\t%s", value, key);
 =A0}
 }

 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=

 assuming I am doing something wrong. There isn't any actual mention in th=

 book of *how* reading in the text file should be accomplished, so what is=

 best way to do this?

 std.file?

 Seems silly providing a program that analyses a text file, without tellin=

 reader how to read in the text file, so I am wondering if there is some
 assumed knowledge I am missing?

 Regards.

Aug 24 2011
prev sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Brian Brady Wrote:

 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?

Now that you know how to use the program, here is the answer to your question. auto content = std.file.readText("filename"); There are other functions depending on use case but this is most common.
Aug 24 2011