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?
– Leonel Sanches da Silva
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)
– Vitor Ferreira
I don’t know if this adaptation is correct, hence my doubt in this algorithm
– Vitor Ferreira
With lists the same method worked perfectly
– Vitor Ferreira