Read and display a complete binary file on the screen c #

0

I'm doing an exercise for class, in which I have to show a binary file on the screen just after storing something in it, the problem is that I always get an error when I get to the end of the file, and what I'm missing , is the condition of the while, to see if someone can help me:

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)
        {
            double b, a, resultat;

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

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


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

                        writer.Write(a);

                        resultat = (0.5)*(b*a);

                        Console.WriteLine("Resultat: " + resultat);
                        writer.Write(resultat);

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

                    using (BinaryReader reader = new BinaryReader(File.OpenRead("calculs.dat")))
                    {
                        Console.Clear();

                        Console.WriteLine("Fitxer de càlculs:\n----------------------------------------");


                        while (reader != ) //Aquí no se hasta qué tengo que leer
                        {
                            Console.WriteLine("Base: " + reader.ReadDouble());
                            Console.WriteLine("Altura: " + reader.ReadDouble());
                            Console.WriteLine("Resultat: " + reader.ReadDouble());
                            Console.WriteLine("---------------------------------------");
                        }

                    }
                }
                else
                {
                    using (BinaryWriter writerElse = new BinaryWriter(File.Open("calculs.dat", FileMode.Create)))
                    { 
                        Console.WriteLine("Fitxer creat correctament");

                        writerElse.Write(b);

                        writerElse.Write(a);


                        resultat = (0.5) * (b * a);

                        Console.WriteLine("Resultat: " + resultat);
                        writerElse.Write(resultat);

                        Console.WriteLine("Polsa una tecla per continuar");
                        Console.Read();
                    }

                    using (BinaryReader readerElse = new BinaryReader(File.OpenRead("calculs.dat")))
                    {
                        Console.Clear();
                        Console.WriteLine("Fitxer de càlculs:\n----------------------------------------");

                        while (readerElse != ) //Aquí igual
                        {
                            Console.WriteLine("Base: " + readerElse.ReadDouble());
                            Console.WriteLine("Altura: " + readerElse.ReadDouble());
                            Console.WriteLine("Resultat: " + readerElse.ReadDouble());
                            Console.WriteLine("---------------------------------------");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error general");
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

        } //end Main
    }//end class
}//end namespace
  

The error says that it can not be read beyond the sequence

    
asked by THR4SH3RP0L0 30.05.2017 в 16:38
source

1 answer

1

Within the condition of while you should check that you are not in the last position, for this you can do it like this:

using (BinaryReader reader = new BinaryReader(File.OpenRead("calculs.dat")))
{
    while (reader.BaseStream.Position != reader.BaseStream.Length) 
    {
        //Aquí debes hacer el reader.ReadDouble(); pero tienes que tener en cuenta que solo 
        //puedes hacer uno por cada loop del reader ya que cada vez que lo haces lees 
        //8 bytes y los avanzas dentro del reader por lo que la comprobación que hacemos en
        // el while no contempla esa posibilidad, siquieres hacer 3 readDouyble en el mismo loop comprueba antes que la posicion en la que estas te permita avanzar 24bytes sin que sea el final.
        Console.WriteLine( reader.ReadDouble());

    }

}
    
answered by 30.05.2017 / 16:52
source