How to do that would in a typed file, the data is recorded without erasing what is already recorded and display everything on the screen?

0 like 0 dislike
I did
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace WorkWithFile2 { class Program { [SerializableAttribute] struct Schedule { // id of the flight public string idPoleta; // direction public string flightWay; // date of departure public DateTime airDate; // the duration of the flight public int durationOfFlight; // the number of seats available public int freePlaces; } static void Main(string[] args) { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(@"C:\\Temp\\schedule.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); Schedule shd; int choise = 1; while (choise != 0 ) { Console.WriteLine("[1] - Add new flight"); Console.WriteLine("[2] - Show schedule."); Console.WriteLine("[0] Exit"); Console.Write("Choice: "); choise = int.Parse(Console.ReadLine()); switch (choise) { case 1: { Console.WriteLine("Add new flight"); Console.Write("Enter flight ID: "); shd.idPoleta = Console.ReadLine(); Console.Write("Enter the direction of flight: "); shd.flightWay = Console.ReadLine(); Console.Write("Enter the date and time of departure (approx. May 12 2016 12:36:55): "); shd.airDate = DateTime.Parse(Console.ReadLine()); Console.Write("Enter the flight duration in hours (approx.: 12): "); shd.durationOfFlight = int.Parse(Console.ReadLine()); Console.Write("Enter the number of available seats: "); shd.freePlaces = int.Parse(Console.ReadLine()); bf.Serialize(fs, shd); fs.Flush(); fs.Seek(0, SeekOrigin.Begin); shd = (Schedule)bf.Deserialize(fs); Console.WriteLine("Reis added."); Console.WriteLine("\\tID: {0}", shd.idPoleta); Console.WriteLine("\\tНаправление flight: {0}", shd.flightWay); Console.WriteLine("\\tДата and time of departure: {0}", shd.airDate.ToString("dd.MM.yyyy HH:mm")); Console.WriteLine("\\tПродолжительность flight: {0} hours", shd.durationOfFlight); Console.WriteLine("\\tКоличество seats available: {0}", shd.freePlaces); fs.Close(); break; } } } } } }

But then each new addition erases what is already added. How to make an Appendix with an existing and then display everything on the console screen?

Thanks for the help!
by | 7 views

2 Answers

0 like 0 dislike
Need FileMode.Append
fs.Seek(0, SeekOrigin.Begin);
What if next file still work?
shd = (Schedule)bf.Deserialize(fs);
What if shd already has everything?
0 like 0 dislike
There is a small feature, if not add to end of file new object. Use the class
List schedules;
1.Before the add can read the entire file and record in schedules.
2.To add a new object in schedules.Add(new Schedules())
3. To sterilitate and save List

This approach is not desirable if you have a very large file. But with this approach you won't have to worry that you can't add to end of file new object...

P. S a little bit about the serialization here

Related questions

110,608 questions
257,186 answers
33,855 users