tags:

views:

74

answers:

3

I've been working on my module exercises and I came across this code snippet which reads the notepad file and prints the details about it.

It's working fine, but I just want to know how to give the path of the notepad file in the code itself other than giving the path in the command line.

Below is my code.

 class Module06
{
   public static void Exercise01(string[] args)
    {
        string fileName = args[0];
        FileStream stream = new FileStream(fileName, FileMode.Open);
        StreamReader reader = new StreamReader(stream);
        int size = (int)stream.Length;
        char[] contents = new char[size];
        for (int i = 0; i < size; i++)
        {
            contents[i] = (char)reader.Read();
        }
        reader.Close();
        Summarize(contents);
    }

    static void Summarize(char[] contents)
    {
        int vowels = 0, consonants = 0, lines = 0;
        foreach (char current in contents)
        {
            if (Char.IsLetter(current))
            {
                if ("AEIOUaeiou".IndexOf(current) != -1)
                {
                    vowels++;
                }
                else
                {
                    consonants++;
                }
            }
            else if (current == '\n')
            {
                lines++;
            }
        }
        Console.WriteLine("Total no of characters: {0}", contents.Length);
        Console.WriteLine("Total no of vowels    : {0}", vowels);
        Console.WriteLine("Total no of consonants: {0}", consonants);
        Console.WriteLine("Total no of lines     : {0}", lines);
    }
}
+1  A: 

Reading of a text file is much easier with File.ReadAllText then you don't need to think about closing the file you just use it. It accepts file name as parameter.

string fileContent = File.ReadAllText("path to my file");
Perica Zivkovic
A: 
string fileName = @"path\to\file.txt";
Danielvg
+2  A: 

In your static void Main, call

string[] args = {"filename.txt"};
Module06.Exercise01(args);
KennyTM