c# error

I have a couple of questions.

1) i wrote a program that takes a file and counts the words; however the compiler is not recognizing the text file.  

when write wordcount words(a text file in the same project), the compiler is not recognizing the file words.

2) What do i do if i want the compiler to do wordcount for two files

if i want to run the program as wordcount words1 words2 (two text files in the same project.
asif07Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HardiCommented:
Using the function from
http://www.thejackol.com/2006/06/23/function-to-count-words-in-c-aspnet/
here is how to get a word count of a text file.
using System;
using System.Collections.Generic; 
using System.IO; 
using System.Text;
 
public class Test
{
    static void Main()
    {
        StreamReader sr = new StreamReader("file.txt");
        string line;
        int wordcount = 0;
 
        while(!sr.EndOfStream)
        {
            line = sr.ReadLine();
            wordcount += WordCount(line);
        }
 
        Console.WriteLine("Word count = "+wordcount);
        Console.Read();
    }
 
    public static int WordCount(string Text)
    {
        string tmpStr;
 
        tmpStr = Text.Replace("\t", " ").Trim();
        tmpStr = tmpStr.Replace("\n", " ");
        tmpStr = tmpStr.Replace("\r", " ");
 
        while (tmpStr.IndexOf("  ") != -1)
            tmpStr = tmpStr.Replace("  ", " ");
 
        return tmpStr.Split(' ').Length;
    }
 
}

Open in new window

0
AUmidhCommented:
string strFile = File.ReadAllText("C:\\log.txt");
int wordcount=0;
and then use Hardi method

wordcount=WordCount(strFile);
public static int WordCount(string Text)
    {
        string tmpStr;
 
        tmpStr = Text.Replace("\t", " ").Trim();
        tmpStr = tmpStr.Replace("\n", " ");
        tmpStr = tmpStr.Replace("\r", " ");
 
        while (tmpStr.IndexOf("  ") != -1)
            tmpStr = tmpStr.Replace("  ", " ");
 
        return tmpStr.Split(' ').Length;
    }
0
patrickrivaCommented:
Since you talk about "program" I guess you have a console application
and then this is probably what you need
private void CountWords(string fileName)
{
	...
}
 
static void Main ( string[] args )
{
	int total = 0;
	foreach (string s in args)
	{
		int words = CountWords(s);
		Console.WriteLine("File: %0   Words: %1", s, words);
		total += words;
	}
	Console.WriteLine();
	Console.WriteLine("Total Words: %0", total);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HardiCommented:
Alright I forgot to answer your question #2, but didn't I answer the first one?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.