Table + array + files C# Exercise - C# Programming Course

 Exercise

Table + array + files

 Objetive

Expand the exercise (tables + array) by adding two new methods to dump the data of the array into a binary file and restore the data from the file.

 Example Code

// Importing necessary namespaces
using System; // To use general .NET functionalities
using System.IO; // To perform file I/O operations

// Define a class to represent the structure of the table
class Table
{
    public string Name { get; set; } // Name of the table
    public string Description { get; set; } // Description of the table
    public int[] Data { get; set; } // Array to store table data

    // Constructor to initialize a new Table with a name, description, and data
    public Table(string name, string description, int[] data)
    {
        Name = name; // Assign name to the table
        Description = description; // Assign description to the table
        Data = data; // Assign the array of data to the table
    }

    // Method to dump the table's data into a binary file
    public void DumpToBinaryFile(string fileName)
    {
        try
        {
            // Open a new binary file for writing
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                // Write the table name to the file
                writer.Write(Name);
                // Write the table description to the file
                writer.Write(Description);
                // Write the size of the data array
                writer.Write(Data.Length);

                // Write each element of the data array to the file
                foreach (var item in Data)
                {
                    writer.Write(item); // Write individual elements of the data array
                }
            }

            // Notify that the data has been written successfully
            Console.WriteLine("Data dumped successfully to the binary file.");
        }
        catch (Exception ex)
        {
            // Display error message if any exception occurs
            Console.WriteLine($"Error writing to binary file: {ex.Message}");
        }
    }

    // Method to restore the table's data from a binary file
    public static Table RestoreFromBinaryFile(string fileName)
    {
        try
        {
            // Open the binary file for reading
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                // Read the table name
                string name = reader.ReadString();
                // Read the table description
                string description = reader.ReadString();
                // Read the size of the data array
                int length = reader.ReadInt32();

                // Initialize an array to hold the data
                int[] data = new int[length];

                // Read the data array from the file
                for (int i = 0; i < length; i++)
                {
                    data[i] = reader.ReadInt32(); // Read each integer from the file
                }

                // Return a new Table object with the restored data
                return new Table(name, description, data);
            }
        }
        catch (Exception ex)
        {
            // Display error message if any exception occurs
            Console.WriteLine($"Error reading from binary file: {ex.Message}");
            return null;
        }
    }
}

// Main class to demonstrate the Table and array functionality
class Program
{
    static void Main(string[] args)
    {
        // Example data to be used in the table
        int[] tableData = new int[] { 1, 2, 3, 4, 5 };

        // Create a new table instance
        Table table = new Table("Table1", "Example Table Description", tableData);

        // Dump the table data to a binary file
        table.DumpToBinaryFile("tableData.bin");

        // Restore the table data from the binary file
        Table restoredTable = Table.RestoreFromBinaryFile("tableData.bin");

        // Check if the table has been restored successfully
        if (restoredTable != null)
        {
            // Display the restored table data
            Console.WriteLine($"Restored Table Name: {restoredTable.Name}");
            Console.WriteLine($"Restored Table Description: {restoredTable.Description}");
            Console.WriteLine("Restored Data:");
            foreach (var item in restoredTable.Data)
            {
                Console.Write(item + " "); // Display each item from the restored data array
            }
        }
    }
}

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.