Are there large differences in the "binary recording" of a file, when entering lists or dictionaries?

Asked

Viewed 47 times

2

I’m developing a language project C# where I need to use files to store the data circulating in the data structures.

Turns out I had the whole project all worked out, I wasn’t layering. I was previously resorting to Lists (data structure) and to record the Data the methods that below are worked perfectly

 public static bool GravaCarrosEstacionados(string filename,Dictionary<string,Park> carros)
        {

            try
              {
                 Stream stream = File.Open(filename, FileMode.Create);
                 BinaryFormatter bin = new BinaryFormatter();
                 bin.Serialize(stream, carros);
                 stream.Close();
                 return true;
              }
              catch (IOException e)   
              {
                  Console.Write("ERRO:" + e.Message);
                  return false;
              }
        }


 public static bool CarregarCarrosEstacionados(string fileName,string nomeP)
        {
            if (File.Exists(fileName))
            {
                try
                {
                    Stream stream = File.Open(fileName, FileMode.Open);
                    BinaryFormatter bin = new BinaryFormatter();
                    parques = (Dictionary<string,Park>)bin.Deserialize(stream);
                    stream.Close();

                    foreach (KeyValuePair<string, Park> x in parques)
                    {
                        if (parques.ContainsKey(nomeP))
                        {
                            Console.WriteLine("Carro {0}", x.Value.Carro.Marca);
                            Console.WriteLine("Modelo {0}", x.Value.Carro.Modelo);
                            Console.WriteLine("Matricula {0}", x.Value.Carro.Matricula);


                        }

                    }

                }
                catch (IOException e)
                {
                    Console.Write("ERRO:" + e.Message);
                    return false;
                }


            }
            return true;

        }

Obviously it applied lists instead of dictionaries during the "serialize" and the "Deserialize". Now with the dictionary adding for example a car to the system I can’t even save the file.

What might be happening differently?

  • What are the errors that occur?

  • 1

    I notice that the file is not saved when the Record Carosestacionados(string filename,Dictionary<string,Park> cars) method is called above (in another method)

  • 1

    I don’t know if this adaptation is correct, hence my doubt in this algorithm

  • 1

    With lists the same method worked perfectly

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.