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?
 
patrickrivaConnect With a Mentor Commented:
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
 
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
 
HardiCommented:
Alright I forgot to answer your question #2, but didn't I answer the first one?
0
All Courses

From novice to tech pro — start learning today.