If you are doing this using C#, read the file into a string or a buffer and then apply a regular expression to locate words within the file's content. Finally, just count the matches returned. Faster than looping through all the characters.
using System;
using System.Text.RegularExpressions;
public CountWordsProgram
{
public static int CountWords(string completeText)
{
MatchCollection collection = Regex.Matches(completeText, @"[\S]+");
return collection.Count;
}
public static void Main(string[] args)
{
Console.WriteLine( CountWordsProgram.CountWords(File.ReadAllText( @"filename.txt" )) );
}
}