Question
Why is the following code not working in C# Design a program that reads in an ASCII text file (provided) and creates an output file
Why is the following code not working in C#
Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII character and the number of time the character appears in the file. Do not sort the output. Each unique character in the file must be represented by a character frequency class instance. For this exercise, the character frequency objects must be stored in a vector. For example, given the sample input file: Hello. The output file would look like this: H(72) 1 e(101) 1 l(108) 2 o(111) 1 .(46) 1 The program should be able to be executed from the command line using the following syntax: programname.exe (optional for this assignment).
{ class CharacterFrequency { private char ch; private int frequency;
public char Ch { get { return ch; } set { ch = value; } } public int Frequency { get { return frequency; } set { frequency = value; } } }
class Program { public string InputFileName = ""; public string OutputFileName = "Output.txt"; public string FilePath = "";
public static SortedDictionary
foreach (var character in stringToCount) { if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount { characterCount.Add(character, 1); } else { characterCount[character]++; // increemetned count } } return characterCount; }
static void Main(string[] args) { Program p = new Program(); CharacterFrequency obj = new CharacterFrequency(); StreamWriter streamWriter = new StreamWriter(p.OutputFileName);
try { p.InputFileName = args[0]; // get input p.OutputFileName = args[1]; // get output p.FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + p.InputFileName; // get file path if (File.Exists(p.FilePath)) // checked if file exist { string data = File.ReadAllText(p.FilePath); // read the data in string var count = Program.Count(data); // passthe string to count method foreach (var character in count) { streamWriter.WriteLine(character.Key + "(" + (int)character.Key + ")" + "\t" + character.Value); // write data to output file } streamWriter.Close(); Console.ReadLine(); } else { Console.WriteLine("Please provide input File "); // if input file absent sent message to user to place input file Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine("Exception occured " + ex.Message.ToString()); Console.ReadLine(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started