计算字符在文本文件中出现的次数

时间:2015-12-21 18:34:43

标签: c#

我正在学习C#,我想计算一个字符出现在文本文件中的次数。用户输入文本文件的路径和要搜索的字符。该程序目前没有找到任何字符。我究竟做错了什么?这是代码:

    public static void Main(string[] args)
    {
        string fileName;

        fileName = readInput("Please enter the path to the file: ");
        StreamReader fromFile = new StreamReader(fileName);
        string line;

        while ((line = fromFile.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }

        int count = 0;
        char search;
        search = readSearch("Please enter the character you want to search for: ");

        while ((line = fromFile.ReadLine()) != null)
        {
            foreach (char letter in line)
            {
                if (search == letter)
                {
                    count ++;
                }
            }
        }

        Console.WriteLine("{0} was found {1} time(s).", search, count);
        Console.ReadLine();
        fromFile.Close();
        Console.ReadLine();
    }

    private static string readInput(string questionPath)
    {
        Console.Write(questionPath);
        string input = Console.ReadLine();
        return input;
    }

    private static char readSearch(string questionSearch)
    {
        Console.Write(questionSearch);
        char input = Convert.ToChar(Console.Read());
        return input;
    }
}

3 个答案:

答案 0 :(得分:1)

在计算匹配的字符之前,您似乎正在将文件写入屏幕。这样,您将StreamReader移动到文件末尾,并且不会重置它。每次重置您的阅读器(并使用using):

StreamReader fromFile;
using(fromFile = new StreamReader(fileName))
{
     string line;
     while ((line = fromFile.ReadLine()) != null)
     {
         Console.WriteLine(line);
     }
}

int count = 0;
char search;
search = readSearch("Please enter the character you want to search for: ");

using(fromFile = new StreamReader(fileName))
{
     while ((line = fromFile.ReadLine()) != null)
     {
        //....
     }
}

或者您最初可以将文件读入内存。

答案 1 :(得分:0)

问题是您已经阅读了整个流。一个简单的解决方法是从头重新开始读取。这可以通过在开始计数操作之前执行以下操作来完成:

user_id

作为额外的信息,您可以使用LINQ语句替换计数操作。替换:

        fromFile.BaseStream.Seek(0, SeekOrigin.Begin);

使用:

            foreach (char letter in line)
            {
                if (search == letter)
                {
                    count++;
                }
            }

答案 2 :(得分:0)

此代码将打开一个文件并计算其中每个字符的数量,并将总计输出到另一个文件。

            if (File.Exists(charCountFileName)) File.Delete(charCountFileName);

            SortedDictionary<ushort, int> charsDict = new SortedDictionary<ushort, int>();

            using (StreamReader r = new StreamReader(inputFileName))
            {
                char[] buffer = new char[1024];
                int read;
                while ((read = r.ReadBlock(buffer, 0, buffer.Length)) > 0)
                {
                    for (int i = 0; i < read; i++)
                    {
                        if (!charsDict.ContainsKey((ushort)buffer[i]))
                        {
                            charsDict.Add((ushort)buffer[i], 1);
                        }
                        else
                            charsDict[(ushort)buffer[i]]++;
                    }

                }
            }           

            using (StreamWriter file = new StreamWriter(new FileStream(charCountFileName, FileMode.Create), Encoding.UTF8))
                foreach (var entry in charsDict)
                    file.WriteLine("{0}\t{1}\t{2}", entry.Key, Convert.ToChar(entry.Key), entry.Value);