B
B
bpGusar2018-04-30 13:43:45
C++ / C#
bpGusar, 2018-04-30 13:43:45

How to make data written in a typed file without erasing what is already written and display everything on the screen?

I did so

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 рейса
            public string idPoleta;
            // направление
            public string flightWay;
            // дата вылета
            public DateTime airDate;
            // продолжительность полета
            public int durationOfFlight;
            // количество свободных мест
            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] - Добавить новый рейс");
                Console.WriteLine("[2] - Показать распиание");
                Console.WriteLine("[0] - Выход");
                Console.Write("Выбор: ");
                choise = int.Parse(Console.ReadLine());
                switch (choise)
                {
                    case 1:
                        {
                            Console.WriteLine("Добавить новый полет");

                            Console.Write("Введите ID рейса: ");
                            shd.idPoleta = Console.ReadLine();

                            Console.Write("Введите направление рейса: ");
                            shd.flightWay = Console.ReadLine();

                            Console.Write("Введите дату и время вылета (прим.: 12 мая 2016 12:36:55): ");
                            shd.airDate = DateTime.Parse(Console.ReadLine());

                            Console.Write("Введите продолжительность полета в часах (прим.: 12): ");
                            shd.durationOfFlight = int.Parse(Console.ReadLine());

                            Console.Write("Введите количество свободных мест: ");
                            shd.freePlaces = int.Parse(Console.ReadLine());

                            bf.Serialize(fs, shd);

                            fs.Flush();

                            fs.Seek(0, SeekOrigin.Begin);

                            shd = (Schedule)bf.Deserialize(fs);

                            Console.WriteLine("Рейс добавлен.");
                            Console.WriteLine("\tID рейса: {0}", shd.idPoleta);
                            Console.WriteLine("\tНаправление рейса: {0}", shd.flightWay);
                            Console.WriteLine("\tДата и время вылета: {0}", shd.airDate.ToString("dd.MM.yyyy HH:mm"));
                            Console.WriteLine("\tПродолжительность полета: {0} часов", shd.durationOfFlight);
                            Console.WriteLine("\tКоличество свободных мест: {0}", shd.freePlaces);

                            fs.Close();
                            break;
                        }
                }
            }
        }
    }
}

But here each new addition erases what has already been added. How to make an addition with an existing one and then display everything that is on the console screen?
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2018-04-30
@Taraflex

FileMode.OpenOrCreate
Need FileMode.Append
https://msdn.microsoft.com/en-us/library/system.io...
Why?
Why if we are still working with the file?
Why, if everything is already in shd?

M
mefutu, 2018-05-07
@mefutu

There is a small feature if it is not possible to add a new object to the end of the file. Use class
1. Before adding, you can read the entire file and write to schedules.
2. Add a new object to 3. Serialize and save already This approach is not desirable if you have a very large file. But with this approach, you will no longer worry that you cannot add a new object to the end of the file ... PS a little about serialization hereschedules.Add(new Schedules())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question