'Unidentifiable character in stream reader

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source