Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算文件的总字符数_C# - Fatal编程技术网

C# 计算文件的总字符数

C# 计算文件的总字符数,c#,C#,嗨,我对C#还不太熟悉,我想做一些练习来跟上它。我试图计算一个文件中的字符总数,但在第一个单词后它就停止了,有人能告诉我哪里出错了吗?提前谢谢 public void TotalCharacterCount() { string str; int count, i, l; count = i = 0; StreamReader reader = File.OpenText("C:

嗨,我对C#还不太熟悉,我想做一些练习来跟上它。我试图计算一个文件中的字符总数,但在第一个单词后它就停止了,有人能告诉我哪里出错了吗?提前谢谢

 public void TotalCharacterCount()
        {
            string str;
            int count, i, l;
            count  = i = 0;

            StreamReader reader = File.OpenText("C:\\Users\\Lewis\\file.txt");
            str = reader.ReadLine();
            l = str.Length;


                while (str != null && i < l)
                {

                    count++;

                    i++;

                    str = reader.ReadLine();
                }



            reader.Close();
            Console.Write("Number of characters in the file is : {0}\n", count);


        }
public void TotalCharacterCount()
{
字符串str;
整数计数,i,l;
计数=i=0;
StreamReader reader=File.OpenText(“C:\\Users\\Lewis\\File.txt”);
str=reader.ReadLine();
l=结构长度;
while(str!=null&&i
如果您想知道文件的大小:

long length = new System.IO.FileInfo("C:\\Users\\Lewis\\file.txt").Length;
Console.Write($"Number of characters in the file is : {length}");
如果您想计算使用C#时使用的字符数,那么下面的一些示例代码可能会对您有所帮助

            int totalCharacters = 0;

            // Using will do the reader.Close for you. 
            using (StreamReader reader = File.OpenText("C:\\Users\\Lewis\\file.txt"))
            {
                string str = reader.ReadLine();

                while (str != null)
                {
                    totalCharacters += str.Length;
                    str = reader.ReadLine();
                }
            }

            // If you add the $ in front of the string, then you can interpolate expressions 
            Console.Write($"Number of characters in the file is : {totalCharacters}");

它在第一个单词后停止

这是因为您在循环中有check
&&i
,然后递增它,这样检查就不会通过。您不必更改l变量的值(顺便说一下,名称不是很好,我确定它是
1
,而不是
l

然后,如果需要获取文件中的字符总数,可以将整个文件读入一个字符串变量,然后从count()Length获取

获取FileInfo的Length属性将给出一个值,该值不必等于字符数(取决于编码一个字符可能需要超过一个字节)

关于你的阅读方式,这也取决于你是否想计算新的线条符号和其他符号

考虑以下示例

static void Main(string[] args)
{
    var sampleWithEndLine = "a\r\n";
    var length1 = "a".Length;
    var length2 = sampleWithEndLine.Length;
    var length3 = @"a
".Length;

    Console.WriteLine($"First sample: {length1}");
    Console.WriteLine($"Second sample: {length2}");
    Console.WriteLine($"Third sample: {length3}");
    var totalCharacters = 0;
    File.WriteAllText("sample.txt", sampleWithEndLine);

    using(var reader = File.OpenText("sample.txt"))
    {
        string str = reader.ReadLine();

        while (str != null)
        {
            totalCharacters += str.Length;
            str = reader.ReadLine();
        }
    }

    Console.WriteLine($"Second sample read with stream reader: {totalCharacters}");

    Console.ReadKey();
}

对于第二个示例,首先,长度将返回3,因为它实际上包含三个符号,而对于流读取器,您将得到1,因为

那么,为什么要检查i>0?当您在单独的行上有变量时,它的可读性也更高,而不是int count,i,l;计数=i=0;但int I=0;int l=0;int计数=i;也许这就是为什么你在第一个单词后就打断了。不确定发生了什么,但我正试图投票给你的答案,但结果是-1,出于某种原因,非常感谢刘易斯!继续说C#,它是一种很好的语言,可以构建非常大的东西:)Viktor,这已经进入了低质量的答案审查队列,因为它没有解释为什么它会是一个答案。我知道你写了很多评论,你可能认为这些就足够了。然而,评论往往会随着时间的推移而被删除,并且不是每个使用该网站的人都会将这些评论与该贡献联系起来。请使用此答案下方的链接添加解释性信息,说明这解决问题的原因,并说明它与已标记为“答案”的答案有何区别。
static void Main(string[] args)
{
    var sampleWithEndLine = "a\r\n";
    var length1 = "a".Length;
    var length2 = sampleWithEndLine.Length;
    var length3 = @"a
".Length;

    Console.WriteLine($"First sample: {length1}");
    Console.WriteLine($"Second sample: {length2}");
    Console.WriteLine($"Third sample: {length3}");
    var totalCharacters = 0;
    File.WriteAllText("sample.txt", sampleWithEndLine);

    using(var reader = File.OpenText("sample.txt"))
    {
        string str = reader.ReadLine();

        while (str != null)
        {
            totalCharacters += str.Length;
            str = reader.ReadLine();
        }
    }

    Console.WriteLine($"Second sample read with stream reader: {totalCharacters}");

    Console.ReadKey();
}