テキストファイル、つまりこれから単語数を数えようとしています。
これはワードカウントプログラムのテストです。これは単なるテストです。プログラムが正常に動作する場合、このファイルには 30 語が含まれていると計算する必要があります。
StreamReader を使用してファイルのすべてを文字列に入れ、.Split メソッドを使用して個々の単語の数を取得していますが、プログラムをコンパイルして実行すると、間違った値を取得し続けます。
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,.";
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
}
fields = line.Split(delim.ToCharArray());
for(int i = 0; i < fields.Length; i++)
{
counter++;
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}