rendered paste bodyusing System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
namespace TextFileStats
{
class Program
{
static void Main(string[] args)
{
string file = "placeholder";
bool isValid = false;
if (args.Length > 0)
{
if (File.Exists(args[0]))
{
file = args[0];
isValid = true;
}
}
while (!isValid)
{
Console.Write("Enter the path to the file: ");
file = Console.ReadLine();
if (!File.Exists(file))
{
Console.WriteLine("The specified file \"{0}\" does not exist!", file);
Console.WriteLine("Press [ ENTER ] to re-enter");
Console.ReadLine();
}
else
{
isValid = true;
}
Console.Clear();
}
Dictionary<char, int> characters = new Dictionary<char, int>();
Dictionary<string, int> words = new Dictionary<string, int>();
string[] lines = File.ReadAllLines(file);
Console.WriteLine("Read file {0} with {1} lines of text", file, lines.Length);
foreach (string str in lines)
{
foreach (char ch in str)
{
if (characters.ContainsKey(ch))
{
characters[ch] += 1;
}
else
{
characters.Add(ch, 1);
}
}
string[] wordArray = str.Split(' ');
foreach (string curStr in wordArray)
{
if (words.ContainsKey(curStr))
{
words[curStr] += 1;
}
else
{
words.Add(curStr, 1);
}
}
}
Console.WriteLine();
Console.WriteLine("Unique Words Read: {0}", words.Count);
Console.WriteLine("Unique Characters Read: {0}", characters.Count);
Console.WriteLine("Please wait... Generating statistics...");
Console.WriteLine();
TextWriter tw = new StreamWriter("statistics.txt");
tw.WriteLine("-- Frequency Statistics Start --");
tw.WriteLine("-- Word Count Statistics Start --");
tw.WriteLine();
foreach (KeyValuePair<string,int> entry in words)
{
tw.WriteLine(entry.Key + " = " + entry.Value);
}
tw.WriteLine();
tw.WriteLine("-- Word Count Statistics End --");
tw.WriteLine("-- Character Count Statistics Start --");
tw.WriteLine();
foreach (KeyValuePair<char,int> entry in characters)
{
tw.WriteLine(entry.Key + " = " + entry.Value);
}
tw.WriteLine();
tw.WriteLine("-- Character Count Statistics End --");
tw.WriteLine("-- Frequency Statistics End --");
tw.Close();
Console.WriteLine("Detailed statistics generated at file location: \n" +
Directory.GetCurrentDirectory() + "\\statistics.txt");
Console.WriteLine();
Console.WriteLine("Press [ ENTER ] to exit");
Console.ReadLine();
}
}
}