流读取器中出现无法识别的字符

发布于 2025-01-15 18:59:20 字数 4657 浏览 3 评论 0原文

我的目标也是收集重复的字符并在文本文件中缩短它们,所以, aaaaa 将导致 5a 和/或 aabbb 将导致 2a3b

在行尾,循环应该检测到 /n 字符,然后结束该行。问题是它找到了一个在原始文本文件和新行之前的新 CoMpReSsEd 中未出现的字符。

如果我在文件中给程序这个,

aaaaa
bbccc

它会在新文件中输出这个,

5a1
2b3c1
1

我认为这可能是一些流氓空间,但我检查了文件,在 1 之后什么都没有。 然后我想这可能是一个空值,所以我添加了一个 if 语句来检查 0x0,它没有返回任何内容。

        char CurrentCharacter = ' ';
        char PreviousCharacter = ' ';
        bool newline = false;
        bool FoundSymbol = false;
        string CurrentLine = "";

        int Counter = 1;

        for (int y = 0; y < Lines-1; y++) //minus one line as one is meta data
        {
            newline = false;
            CurrentCharacter = ' ';
            PreviousCharacter = 'n'; //n just means ignore character before basically this is appropiate when a new line happens or a different symbol
            CurrentLine = "\n";

            Console.WriteLine("New Line starting");
            while (newline == false)
            {
                //reading lines

                FoundSymbol = false;

                Console.WriteLine("Finding new Symbol");
                Counter = 1;

                while (FoundSymbol == false)
                {
                    CurrentCharacter = (char)FileReader.Read();

                    if (CurrentCharacter == PreviousCharacter)
                    {
                        //same character ++ to counter
                        Counter++;

                        Console.WriteLine("Same Character: {0} {1}" ,Counter ,CurrentCharacter);
                    }

                    else if (PreviousCharacter == 'n')  //is previous character as this what its comparing it too
                    {
                        //nothing its just reading the first character of the new symbol
                        //theres probably a better way than using n as a placeholder
                        Console.WriteLine("Placeholder Previous");
                        Console.WriteLine("Current Char: {0}", CurrentCharacter);
                    }

                    else if (CurrentCharacter == '\n')
                    {
                        //theres a new line so create symbol for current character and count and reset loop
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                                                   
                        newline = true;
                        FoundSymbol = true;

                        Console.WriteLine("End of Line");
                    }

                    else if (CurrentCharacter != PreviousCharacter)
                    {
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                        //are not the same so new character symbol is added to list
                        Console.WriteLine("Different Symbol: {0}" ,CurrentCharacter);

                        FoundSymbol = true;
                    }

                    else
                    {
                        Console.WriteLine("What");
                    }

                    
                    PreviousCharacter = CurrentCharacter;
                    Console.WriteLine("New Previous character: {0}" ,PreviousCharacter);

                    Console.ReadLine();
                }
           
            }

            Console.WriteLine("Finished Line {0}: {1}", y, CurrentLine);
            Console.ReadLine();

            File.AppendAllText(SaveLocation ,CurrentLine);
        }

        Console.WriteLine("Done");
        Console.ReadLine();
    }

这是它的代码,忽略与元数据有关的任何内容,因为这只是第一行上的一些数字,这些数字被复制并工作正常,我的注释已磨损,因此如果您愿意,请忽略这些数字。 这是问题发生处的输出。

输入图片description here

然后,我在 CurrentCharacter != PreviousCharacter 的同一个 if 语句上添加了第二个输出,如此处所示,试图找出它实际上是什么。

                    else if (CurrentCharacter != PreviousCharacter)
                    {
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                        //are not the same so new character symbol is added to list
                        Console.WriteLine("Different Symbol: {0} Test: {1}", CurrentCharacter ,CurrentCharacter);

                        FoundSymbol = true;
                    }

然后,这给了我这个输出

在此处输入图像描述

显然,它完全删除了之前输出的文本,所以我最终变得更加困惑,而我现在就在这里。任何帮助将不胜感激,抱歉,如果这写得不好,老实说,我有点害怕在这里问问题,lmao。

My goal is too collect duplicate characters and shorten them in a text file so,
aaaaa will result in 5a and or aabbb will result in 2a3b

At the end of line the loop is supposed to detect the /n character and then end the line. The problem is it finds a character which is not seen in the original text file and the new CoMpReSsEd one before the new line.

If I give the program this in the file

aaaaa
bbccc

It outputs this in the new file

5a1
2b3c1
1

I thought it could be some rogue space but I checked the file and after the 1 there is nothing.
Then thought maybe it was something like a null value so I added an if statement to check for 0x0 which didn't return anything.

        char CurrentCharacter = ' ';
        char PreviousCharacter = ' ';
        bool newline = false;
        bool FoundSymbol = false;
        string CurrentLine = "";

        int Counter = 1;

        for (int y = 0; y < Lines-1; y++) //minus one line as one is meta data
        {
            newline = false;
            CurrentCharacter = ' ';
            PreviousCharacter = 'n'; //n just means ignore character before basically this is appropiate when a new line happens or a different symbol
            CurrentLine = "\n";

            Console.WriteLine("New Line starting");
            while (newline == false)
            {
                //reading lines

                FoundSymbol = false;

                Console.WriteLine("Finding new Symbol");
                Counter = 1;

                while (FoundSymbol == false)
                {
                    CurrentCharacter = (char)FileReader.Read();

                    if (CurrentCharacter == PreviousCharacter)
                    {
                        //same character ++ to counter
                        Counter++;

                        Console.WriteLine("Same Character: {0} {1}" ,Counter ,CurrentCharacter);
                    }

                    else if (PreviousCharacter == 'n')  //is previous character as this what its comparing it too
                    {
                        //nothing its just reading the first character of the new symbol
                        //theres probably a better way than using n as a placeholder
                        Console.WriteLine("Placeholder Previous");
                        Console.WriteLine("Current Char: {0}", CurrentCharacter);
                    }

                    else if (CurrentCharacter == '\n')
                    {
                        //theres a new line so create symbol for current character and count and reset loop
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                                                   
                        newline = true;
                        FoundSymbol = true;

                        Console.WriteLine("End of Line");
                    }

                    else if (CurrentCharacter != PreviousCharacter)
                    {
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                        //are not the same so new character symbol is added to list
                        Console.WriteLine("Different Symbol: {0}" ,CurrentCharacter);

                        FoundSymbol = true;
                    }

                    else
                    {
                        Console.WriteLine("What");
                    }

                    
                    PreviousCharacter = CurrentCharacter;
                    Console.WriteLine("New Previous character: {0}" ,PreviousCharacter);

                    Console.ReadLine();
                }
           
            }

            Console.WriteLine("Finished Line {0}: {1}", y, CurrentLine);
            Console.ReadLine();

            File.AppendAllText(SaveLocation ,CurrentLine);
        }

        Console.WriteLine("Done");
        Console.ReadLine();
    }

Here is the code for it, Ignore anything to do with metadata as this is just some numbers on the first line which is copied across and works fine and my comments are scuffed so ignore those if you want.
This was the output from where the issue occurs.

enter image description here

I then added a second output on the same if statement of CurrentCharacter != PreviousCharacter as shown here at some attempt of finding out what it actually is.

                    else if (CurrentCharacter != PreviousCharacter)
                    {
                        CurrentLine += Convert.ToString(Counter) + PreviousCharacter;
                        //are not the same so new character symbol is added to list
                        Console.WriteLine("Different Symbol: {0} Test: {1}", CurrentCharacter ,CurrentCharacter);

                        FoundSymbol = true;
                    }

This then gave me this as an output

enter image description here

Obviously its entirely removed the outputted text before it so I just ended up being more confused and here I am now. Any help would be appreciated ,sorry if this was badly written honestly im kinda scared of asking questions on here lmao.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文