Display a binary file by screen c #

0

I'm doing an exercise for class, in which I'm asked to enter a height and a base to calculate the area of a triangle, and then we have to show that file on the screen, but I do not know how to make the binary writer interpret the entire document , to see if someone throws me a cable, thanks in advance, here the code (If there is something else that is wrong, let us know!)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            int b, a, resultat;

            Console.WriteLine("Entra el Valor de la base:");
            b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Entra el Valor de la altura:");
            a = Convert.ToInt32(Console.ReadLine());


            try
            {
                if (File.Exists("calculs.dat"))
                {
                    using (BinaryWriter writer = new BinaryWriter(File.Open("calculs.dat", FileMode.Append)))
                    {
                        writer.Write("----------------------------------------------------------------------");

                        writer.Write("Base: " + b);

                        writer.Write("Altura: " + a);

                        resultat = (1/2)*(b*a);
                        Console.WriteLine("Resultat: " + resultat);
                        writer.Write("Resultat: " + resultat);

                        Console.WriteLine("Fitxer actualitzat correctament, polsa una tecla per continuar");
                        Console.Read();
                    }

                    using (BinaryReader reader = new BinaryReader(File.Open("calculs.dat", FileMode.Open)))
                    {
                        Console.WriteLine(reader.ReadString());
                        Console.WriteLine(reader.ReadInt32());
                    }
                }
                else
                {
                    using (BinaryWriter writerElse = new BinaryWriter(File.Open("calculs.dat", FileMode.Create)))
                    {

                        writerElse.Write("Fitxer de càlculs");
                        writerElse.Write("----------------------------------------------------------------------");

                        Console.WriteLine("Fitxer creat correctament");

                        writerElse.Write("Base: " + b);

                        writerElse.Write("Altura: " + a);


                        resultat = (1 / 2) * (b * a);
                        Console.WriteLine("Resultat: " + resultat);
                        writerElse.Write("Resultat: " + resultat);

                        Console.WriteLine("Fitxer actualitzat correctament, polsa una tecla per continuar");
                    }

                    using (BinaryReader readerElse = new BinaryReader(File.Open("calculs.dat", FileMode.Open)))
                    {
                        Console.WriteLine(readerElse.ReadString());
                        Console.WriteLine(readerElse.ReadInt32());
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error general");
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

        } //end Main
    }//end class
}//end namespace
    
asked by THR4SH3RP0L0 25.05.2017 в 19:28
source

0 answers