Think I May Have Found a Bug with wc -l
I'm wondering if I may have found a bug with either my system's Java
interpreter or my system's Cygwin Unix emulation. I've written a
program whose contents I show below that produces a file that gives a
different number of lines when passed as an argument to Unix's =93wc =96l=
=94
and when passed to an extremely simple Java program that uses the
<Scanner> class' <hasNextLine()> and <nextLine()> methods to count how
many lines a file has.
I've also written a Java program that uses the same two methods to
simply output to the screen each line from the input file, and have
displayed its output next to the output of the Unix command =93cat=94 to
show the difference between those two outputs. Note the blank line
between the two lines of text.
So apparently Unix's =93wc =96l=94 and =93cat=94 treat a series of two ca=
rriage
returns followed by a new-line as one line separator while <Scanner>'s
<nextLine()> treats those three characters as two line separators.
Why the difference?
Kevin Simonson
###########################################################################=
########
Script started on Sat Mar 19 11:11:56 2011
sh-4.1$ pwd
/cygdrive/c/Users/kvnsmnsn/Java/WclBug
sh-4.1$ ls -F
JvCat.class JvWcl.class WclBreaker.class WclBroken
JvCat.java JvWcl.java WclBreaker.java
sh-4.1$ cat WclBreaker.java
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WclBreaker
{
public static void main ( String[] arguments)
{
if (arguments.length == 3)
{ try
{ PrintWriter breaker
= new PrintWriter
( new BufferedWriter( new
FileWriter( arguments[ 0])));
breaker.println( arguments[ 1] + "\r\r\n" + arguments[ 2]);
breaker.close();
}
catch (IOException excptn)
{ System.err.println
( "Couldn't open file \"" + arguments[ 0] + "\" for
output!");
}
}
else
{ System.out.println( "Usage is");
System.out.println
( " java WclBreaker <broken-file> <first-string> <second-
string>");
}
}
}
sh-4.1$ cat JvWcl.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class JvWcl
{
public static void main ( String[] arguments)
{
if (0 < arguments.length)
{ int lineCount;
Scanner scnnr;
for (int arg = 0; arg < arguments.length; arg++)
{ try
{ scnnr = new Scanner( new File( arguments[ arg]));
for (lineCount = 0; scnnr.hasNextLine(); lineCount++)
{ scnnr.nextLine();
}
scnnr.close();
System.out.println( lineCount + " " + arguments[ 0]);
}
catch (FileNotFoundException excptn)
{ System.err.println
( "JvWcl: " + arguments[ arg] + ": No such file or
directory");
}
}
}
else
{ System.out.println( "Usage is\n java JvWcl <file-name>+");
}
}
}
sh-4.1$ cat JvCat.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class JvCat
{
public static void main ( String[] arguments)
{
if (0 < arguments.length)
{ Scanner scnnr;
int arg = 0;
try
{ while (arg < arguments.length)
{ scnnr = new Scanner( new File( arguments[ arg++]));
while (scnnr.hasNextLine())
{ System.out.println( scnnr.nextLine());
}
scnnr.close();
}
}
catch (FileNotFoundException excptn)
{ System.err.println
( "Couldn't find file \"" + arguments[ arg - 1] + "\"!");
}
}
else
{ System.out.println( "Usage is\n java JvCat <file-name>+");
}
}
}
sh-4.1$ java WclBreaker AbcDef.Txt Abc Def
sh-4.1$ wc -l AbcDef.Txt
2 AbcDef.Txt
sh-4.1$ java JvWcl AbcDef.Txt
3 AbcDef.Txt
sh-4.1$ cat AbcDef.Txt
Abc
Def
sh-4.1$ java JvCat AbcDef.Txt
Abc
Def
sh-4.1$ exit
exit
Script done on Sat Mar 19 11:13:55 2011