Reputation: 2046
My goal is to serialize a list of new structs and save it to the same file repeatedly(For example, whenever the list has 5 structs, append the new structs to the same file).
class Program
{
static void Main(string[] args)
{
List<struct_realTime2> list_temp2 = new List<struct_realTime2>(100000);
// ADD 5 new structs to list_temp2
for (int num = 0; num < 5; num++)
{
list_temp2.Add(new struct_realTime2 { indexNum = num,
currentTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffffff"),
currentType = "type" });
}
// WRITE structs
using (var fileStream = new FileStream("file.bin", FileMode.Append))
{
var bFormatter = new BinaryFormatter();
foreach (struct_realTime2 stru in list_temp2)
{
bFormatter.Serialize(fileStream, stru);
}
list_temp2.Clear() // empty the list
}
// READ structs
var list = new List<struct_realTime2>();
using (var fileStream = new FileStream("file.bin", FileMode.Open))
{
var bFormatter = new BinaryFormatter();
while (fileStream.Position != fileStream.Length)
{
list.Add((struct_realTime2)bFormatter.Deserialize(fileStream));
}
}
// PRINT OUT structs in the file
foreach (struct_realTime2 stru in list)
{
string content_struct = stru.indexNum.ToString() + ", " + stru.currentTime;
Console.WriteLine(content_struct);
}
// WRITE list
using (var fileStream = new FileStream("file_list.bin", FileMode.Append))
{
var bFormatter = new BinaryFormatter();
bFormatter.Serialize(fileStream, list_temp2);
}
}
}
[Serializable]
public struct struct_realTime2
{
public int indexNum { get; set; }
public string currentTime { get; set; }
public string currentType { get; set; }
}
< the result >
C:\Users\null\source\repos\ConsoleApp6\ConsoleApp6\bin\Debug>ConsoleApp6.exe
0, 2019-11-10 15:31:52.044207
1, 2019-11-10 15:31:52.047225
2, 2019-11-10 15:31:52.047225
3, 2019-11-10 15:31:52.047225
4, 2019-11-10 15:31:52.047225
C:\Users\null\source\repos\ConsoleApp6\ConsoleApp6\bin\Debug>ConsoleApp6.exe
0, 2019-11-10 15:31:52.044207
1, 2019-11-10 15:31:52.047225
2, 2019-11-10 15:31:52.047225
3, 2019-11-10 15:31:52.047225
4, 2019-11-10 15:31:52.047225
0, 2019-11-10 15:31:55.700680
1, 2019-11-10 15:31:55.703627
2, 2019-11-10 15:31:55.703627
3, 2019-11-10 15:31:55.703627
4, 2019-11-10 15:31:55.703627
It works fine if I append each struct to the file and read them. But I want to avoid appending each struct to the file using a loop, and just want to append the list itself
to the file repeatedly and read the file.
It seems appending the list works because whenever I run the program, the size of file_list.bin
doubles. But how can I read file_list.bin
and make a new list with the structs in the file?
I'd appreciate it if I could get some code for that.
Upvotes: 0
Views: 284
Reputation: 34987
If you want to append data to existing file I recommend using a csv style serialization.
If you serialize a list (an array would be better) then the mirror of
bFormatter.Serialize(fileStream, list_temp2.ToArray());
is
var list = (struct_realTime2[])bFormatter.Deserialize(fileStream));
This may not work if you append new data to an existing file (as opposed to overwriting the file each time).
Upvotes: 2