0

It says that its being used already, but it's being used by a stream reader, and a stream writer. I dont know what it wants me to do, I tried creating the file to no avail still gave me an error.

using System;
using System.IO;

namespace EncoderDecoder
{ 
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(compressor());
            decoder(compressor());
            Console.WriteLine(compare());

        }
        static string compressor()
        {
            string[] lines = System.IO.File.ReadAllLines(@"panda-small.txt");
            string set = "0";
            int count = 0;
            string encoded = "";
            foreach(string Line in lines)
            {
                for (int i = 0; i < Line.Length; i++)
                {
                    if (Line.Substring(i, 1) == set)
                    {
                        count++;
                    }
                    else
                    {
                        encoded += count;
                        encoded += ",";
                        count = 0;
                        if (set == "0")
                        {
                            set = "1";
                            count++;
                        }
                        else
                        {
                            set = "0";
                            count++;
                        }
                    }
                }
            }
            encoded += count;
            return encoded;
        }

        static void decoder(string encoded)
        {
            File.Create("encoder.txt");
            StreamWriter writer = new StreamWriter("encoded.txt");
            string line = "";
            string[] decoderArr = encoded.Split(",");
            string set = "0";
            int count = 0;
            for(int i = 0; i < decoderArr.Length; i++)
            {
                if(decoderArr[i] == "ln" || decoderArr[i] == "")
                {
                    Console.Write(Environment.NewLine);
                }
                else
                {
                    for (int j = 0; j < Convert.ToInt32(decoderArr[i]); j++)
                    {  
                        if(count == 35)
                        {
                            Console.WriteLine();
                            count = 0;
                            writer.WriteLine(line);
                            line = "";
                        }
                        count++;
                        line += set;
                        Console.Write(set);
                    }
                    if (set == "0")
                    {
                        set = "1";
                    }
                    else
                    {
                        set = "0";
                    }
                }
            }
        }

        static string compare()
        {
            StreamReader sr = new StreamReader("panda-small.txt");
            StreamReader sr1 = new StreamReader("encoded.txt");
            if (sr == sr1)
            {
                return "true";
            }
            return "false";
        }
    }
}
Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • 1
    it would help if you used "using" statements for all your IO operations to ensure everything is disposed of properly when complete...a lot of other issues with this code besides that though – Ctznkane525 Mar 09 '20 at 17:11
  • Yeah, these Streams aren't getting cleaned up at all, which is going to cause some headaches. I didn't check if that'll actually fix the error you're seeing, but definitely start there. See: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement – Broots Waymb Mar 09 '20 at 17:13
  • Have a look at [this](https://stackoverflow.com/a/11467483) – Matt.G Mar 09 '20 at 17:13
  • Yup, that was it, didn't even know that you had to close the reader, thank you! – Samuel Rubin Mar 09 '20 at 17:15

0 Answers0