Home

Fundamentals of File Streaming

 

Introduction

File streaming consists of performing one of the routine operations on a file, such as creating it or opening it. This basic operation can be performed using a class called FileStream. You can use a FileStream object to get a stream ready for processing. As one of the most complete classes of file processing of the .NET Framework, FileStream is equipped with all necessary properties and methods. To use it, you must first declare a variable of it. The class is equipped with nine constructors.

One of the constructors (the second) of the FileStream class has the following syntax:

public:
    FileStream(String ^ path, FileMode mode);

This constructor takes as its first argument the name or the file or its path. The second argument specifies the type of operation to perform on the file. Here is an example:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";

    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
					       FileMode::Create);

    return 0;
}

Practical LearningPractical Learning: Creating a Stream

  1. To create a new stream, change the SaveOrder() method in the IceCream.cs file and as follows:
     
    // IceCream3.cpp : main project file.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::IO;
    
    // This class is used to create and manage an ice scream
    // and to process an order
    public ref class CIceCream sealed
    {  
    
        . . . No Change
    	
        void SaveOrder()
        {
            String ^ NameOfFile;
            FileStream ^ stmIceCream =
    		gcnew FileStream(NameOfFile, FileMode::Create);
    
    	. . . No Change
        }
    
        void OpenOrder()
        {
            . . . No Change
        }
    };
    
    int main(array<System::String ^> ^args)
    {
        . . . No Change
    
        Console::WriteLine();
        return 0;
    }
  2. Save the file

Stream Writing

A streaming operation is typically used to create a stream. Once the stream is ready, you can write data to it. The writing operation is perform through various classes. One of these classes is BinaryWriter.

The BinaryWriter class can be used to write values of primitive data types (char, int, float, double, etc). To use a BinaryWriter value, you can first declare its variable. To do this, you would use one of the class' three constructors. One of its constructors (the second) has the following syntax:

public:
    BinaryWriter(Stream ^ output);

This constructor takes as argument a Stream value, which could be a FileStream variable. Here is an example:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";

    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
	                                       FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);

    return 0;
}

Most classes that are used to add values to a stream are equipped with a method called Write. This is also the case for the BinaryWriter class. This method takes as argument the value that must be written to the stream. The method is overloaded so that there is a version for each primitive data type. Here is an example that adds strings to a newly created file:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";

    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
					   FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);

    wrtPersons->Write(L"James Bloch");
    wrtPersons->Write(L"Catherina Wallace");
    wrtPersons->Write(L"Bruce Lamont");
    wrtPersons->Write(L"Douglas Truth");

    return 0;
}

Stream Closing

When you use a stream, it requests resources from the operating system and uses them while the stream is available. When you are not using the stream anymore, you should free the resources and make them available again to the operating system so that other services can use them. This is done by closing the stream.

To close a stream, you can can call the Close() method of the class(es) you were using. Here are examples:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";

    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
					   FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);

    wrtPersons->Write(L"James Bloch");
    wrtPersons->Write(L"Catherina Wallace");
    wrtPersons->Write(L"Bruce Lamont");
    wrtPersons->Write(L"Douglas Truth");

    wrtPersons->Close();
    fstPersons->Close();

    return 0;
}

Practical LearningPractical Learning: Writing to a Stream

  1. To be able to complete a file, change the SaveOrder() method in the IceCream.cs file as follows:
     
    // IceCream3.cpp : main project file.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::IO;
    
    // This class is used to create and manage an ice scream
    // and to process an order
    public ref class CIceCream sealed
    {
    public: 
        . . . No Change
    
    	
        void SaveOrder()
        {
            String ^ strNameOfFile;
                
            Console::Write(L"Please enter your initials or the name "
                           L"we will use to remember your order: ");
            strNameOfFile = Console::ReadLine();
            strNameOfFile = strNameOfFile + L".icr";
                
            // Find out if the user entered a name of a file 
            // that is already in the machine
    		if( File::Exists(strNameOfFile) )
            {
                char answer;
                    
                FileStream ^ stmIceCream =
    			gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceCream =
                        gcnew BinaryWriter(stmIceCream);
    
                // If so, find out if the user wants to 
                // replace the old file
                Console::WriteLine(L"The file you entered exists already.");
                Console::Write(L"Do you want to replace it(y/n)?");
    			answer = char::Parse(Console::ReadLine());
    
                // If the customer wants to replace it...
                if( (answer == L'y') || (answer == 'Y') )
                {
                    // ... do so
                    Console::WriteLine(L"The former order with the same "
                                       L"name will be replaced");
    
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                    bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceCream->Write(Container[ChoiceContainer - 1]);
                    bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceCream->Write(Scoops);
                    bnwIceCream->Write(TotalPrice);
                }
                // If the customer wants to save the new order with 
                // a different name
                else if( (answer == L'n') || (answer == 'N') )
                {
                    // Ask the user to enter a name to remember the order
                    Console::Write(L"Please enter a name we will use "
                                   L"to remember this order: ");
                    strNameOfFile = Console::ReadLine();
                    strNameOfFile = strNameOfFile + L".icr";
    
    		stmIceCream = gcnew FileStream(strNameOfFile, FileMode::Create);
                    bnwIceCream = gcnew BinaryWriter(stmIceCream);
    
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                    bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceCream->Write(Container[ChoiceContainer - 1]);
                    bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceCream->Write(Scoops);
                    bnwIceCream->Write(TotalPrice);
                }
                else
                    Console::WriteLine(L"Invalid Answer - We will close");
    
                bnwIceCream->Close();
                stmIceCream->Close();
            }
            else
            {
                FileStream ^ stmIceCream =
    			gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceCream =
                        gcnew BinaryWriter(stmIceCream);
    
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                bnwIceCream->Write(Container[ChoiceContainer - 1]);
                bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                bnwIceCream->Write(Scoops);
                bnwIceCream->Write(TotalPrice);
    
                bnwIceCream->Close();
                stmIceCream->Close();
            }
        }
    
        void OpenOrder()
        {
            . . . No Change
        }
    };
    
    int main(array<System::String ^> ^args)
    {
        . . . No Change
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application and test it. Here is an example:
     
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Vending Machine
    -----------------------------------
    What type of flavor do you want?
    1 - Vanilla
    2 - Cream of Cocoa
    3 - Chocolate Chip
    4 - Organic Strawberry
    5 - Butter Pecan
    6 - Cherry Coke
    7 - Chocolate Brownies
    8 - Caramel Au Lait
    9 - Chunky Butter
    10 - Chocolate Cookie
    Your Choice? 8
    -----------------------------------
    What type of container do you want?
    1 - Cone
    2 - Cup
    3 - Bowl
    Your Choice? 2
    -----------------------------------
    Do you want an ingredient or not
    1 - No Ingredient
    2 - Peanuts
    3 - M & M
    4 - Cookies
    Your Choice? 3
    -----------------------------------
    How many scoops(1, 2, or 3)? 2
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Order
    -----------------------------------
    Flavor:      Caramel Au Lait
    Container:   Cup
    Ingredient:  M & M
    Scoops:      2
    Total Price: $3.10
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time you come to get your ice scr
    eam (y/n)? Y
    Please enter your initials or the name we will use to remember your order: LS
    
    =-= Ice Scream Vending Machine =-=
     Saving Order: LS.icr
    Press any key to continue . . .
  3. Close the DOS window

Stream Reading

As opposed to writing to a stream, you may want to read existing data from it. Before doing this, you can first specify your intent to the streaming class using the FileMode enumerator. This can be done using the FileStream class as follows:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";
/*
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
					   FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);

    wrtPersons->Write(L"James Bloch");
    wrtPersons->Write(L"Catherina Wallace");
    wrtPersons->Write(L"Bruce Lamont");
    wrtPersons->Write(L"Douglas Truth");

    wrtPersons->Close();
    fstPersons->Close();
*/
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Open);

    return 0;
}

Once the stream is ready, you can get prepared to read data from it. To support this, you can use the BinaryReader class. This class provides two constructors. One of the constructors (the first) has the following syntax:

public:
    BinaryReader(Stream ^ input);

This constructor takes as argument a Stream value, which could be a FileStream object. After declaring a FileStream variable using this constructor, you can read data from it. To do this, you can call an appropriate method. This class provides an appropriate method for each primitive data type.

After using the stream, you should close it to reclaim the resources it was using. This is done by calling the Close() method.

Here is an example of using the mentioned methods:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Persons.spr";
/*
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
					   FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);

    wrtPersons->Write(L"James Bloch");
    wrtPersons->Write(L"Catherina Wallace");
    wrtPersons->Write(L"Bruce Lamont");
    wrtPersons->Write(L"Douglas Truth");

    wrtPersons->Close();
    fstPersons->Close();
*/

    FileStream ^ fstPersons = gcnew FileStream(NameOfFile,
	                                       FileMode::Open);
    BinaryReader ^ rdrPersons = gcnew BinaryReader(fstPersons);
    String ^ strLine = nullptr;

    strLine = rdrPersons->ReadString();
    Console::WriteLine(strLine);
    strLine = rdrPersons->ReadString();
    Console::WriteLine(strLine);
    strLine = rdrPersons->ReadString();
    Console::WriteLine(strLine);
    strLine = rdrPersons->ReadString();
    Console::WriteLine(strLine);

    rdrPersons->Close();
    fstPersons->Close();

    Console::WriteLine();
    return 0;
}

This would produce:

James Bloch
Catherina Wallace
Bruce Lamont
Douglas Truth

Press any key to continue . . .

Practical LearningPractical Learning: Reading From a Stream

  1. To be able to retrieve data from an existing file, change the IceCream.cs file as follows:
     
    // IceCream3.cpp : main project file.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::IO;
    
    // This class is used to create and manage an ice scream
    // and to process an order
    public ref class CIceCream sealed
    {
    public: 
        // This is the base price of an ice scream
        // Optional values may be added to it
    	static const double BasePrice = 1.55;
    
        // These arrays are used to build the components 
        // of various ice screams
    private:
        array<String ^> ^ Flavor;
        array<String ^> ^ Container;
        array<String ^> ^ Ingredient;
    
        // Additional factor used to process an ice scream order
        int Scoops;
        double TotalPrice;
    
        // Variables that will hold the user's choice
        // These are declared "globally" so they can be 
        // shared among methods
        int ChoiceFlavor;
        int ChoiceContainer;
        int ChoiceIngredient;
    
        // This default constructor is the best place for 
        // us to initialize the array
    public:
        CIceCream()
        {
            Flavor = gcnew array<String ^>(10);
            Flavor[0] = L"Vanilla";
            Flavor[1] = L"Cream of Cocoa";
            Flavor[2] = L"Chocolate Chip";
            Flavor[3] = L"Organic Strawberry";
            Flavor[4] = L"Butter Pecan";
            Flavor[5] = L"Cherry Coke";
            Flavor[6] = L"Chocolate Brownies";
            Flavor[7] = L"Caramel Au Lait";
            Flavor[8] = L"Chunky Butter";
            Flavor[9] = L"Chocolate Cookie";
    
            Ingredient = gcnew array<String^>(4);
            Ingredient[0] = L"No Ingredient";
            Ingredient[1] = L"Peanuts";
            Ingredient[2] = L"M & M";
            Ingredient[3] = L"Cookies";
    
            Container = gcnew array<String^>(3);
            Container[0] = L"Cone";
            Container[1] = L"Cup";
            Container[2] = L"Bowl";
        }
    
        // This method requests a flavor from the user and
        // returns the choice
        void ChooseFlavor()
        {
            // Make sure the user selects a valid number 
            //that represents a flavor...
            do {
                // In case the user types a symbol that 
                // is not a number
                try {
                    Console::WriteLine(L"What type of flavor do you want?");
                    for (int i = 0; i < Flavor->Length; i++)
                        Console::WriteLine(L"{0} - {1}", i + 1, Flavor[i]);
                    Console::Write(L"Your Choice? ");
                    ChoiceFlavor = int::Parse(Console::ReadLine());
                }
                catch(FormatException ^)	// display an appropriate message
                {
                    Console::WriteLine(L"You must enter a valid number "
                                       L"and no other character!");
                }
    
                // If the user typed an invalid number out of the 
                // allowed range
                // let him or her know and provide another chance
                if( ChoiceFlavor < 1 || ChoiceFlavor > Flavor->Length)
                    Console::WriteLine(L"Invalid Choice - Try Again!\n");
            } while (ChoiceFlavor < 1 || ChoiceFlavor > Flavor->Length);
        }
    
        // This method allows the user to select a container
        void ChooseContainer()
        {
            // Make sure the user selects a valid number that 
            // represents a container
            do {
                // If the user types a symbol that is not a number
                try
                {
                    Console::WriteLine(L"What type of container do you want?");
                    for (int i = 0; i < Container->Length; i++)
                        Console::WriteLine(L"{0} - {1}", i + 1, Container[i]);
                    Console::Write(L"Your Choice? ");
                    ChoiceContainer = int::Parse(Console::ReadLine());
                }
                catch (FormatException ^)	// display an appropriate message
                {
                    Console::WriteLine(L"You must enter a valid "
                                       L"number and no other character!");
                }
    
                // If the user typed an invalid number out of the 
                // allowed range
                // let him or her know and provide another chance
                if( (ChoiceContainer < 1) ||
                    (ChoiceContainer > Container->Length) )
                    Console::WriteLine(L"Invalid Choice - Try Again!");
            } while ((ChoiceContainer < 1) ||
                     (ChoiceContainer > Container->Length));
        }
    
        void ChooseIngredient()
        {
            do {
                try {
                    Console::WriteLine(L"Do you want an ingredient or not");
                    for (int i = 0; i < Ingredient->Length; i++)
                        Console::WriteLine(L"{0} - {1}",
                                           i + 1, Ingredient[i]);
                    Console::Write(L"Your Choice? ");
                    ChoiceIngredient = int::Parse(Console::ReadLine());
                }
                catch (FormatException ^)
                {
                    Console::WriteLine(L"You must enter a valid "
                                       L"number and no other character!");
                }
    
                if( (ChoiceIngredient < 1) ||
                    (ChoiceIngredient > Ingredient->Length) )
                    Console::WriteLine(L"Invalid Choice - Try Again!");
            } while ((ChoiceIngredient < 1) ||
                     (ChoiceIngredient > Ingredient->Length));
        }
    
        void SpecifyNumberOfScoops()
        {
            do {
                try {
                    Console::Write(L"How many scoops(1, 2, or 3)? ");
                    Scoops = int::Parse(Console::ReadLine());
                }
                catch (FormatException ^)
                {
                    Console::WriteLine(L"You must enter a valid number "
                                       L"and no other character!");
                }
    
                if( Scoops < 1 || Scoops > 3 )
                    Console::WriteLine(L"Invalid Choice - Try Again!");
            } while (Scoops < 1 || Scoops > 3);
        }
    
        // This method is used to process a customer order
        // It uses the values of the above methods
        void ProcessAnOrder()
        {
            double PriceIngredient, PriceScoop;
    
            // Let the user know that this is a vending machine
            Console::WriteLine(L"=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
            Console::WriteLine(L"Ice Scream Vending Machine");
            Console::WriteLine(L"-----------------------------------");
    
            // Let the user select the components of the ice scream
    		ChooseFlavor();
            Console::WriteLine(L"-----------------------------------");
            ChooseContainer();
            Console::WriteLine(L"-----------------------------------");
            ChooseIngredient();
            Console::WriteLine(L"-----------------------------------");
            SpecifyNumberOfScoops();
            Console::WriteLine(L"-----------------------------------");
    
            // If the user selects an ingredient instead of "No Ingredient",
            // add $0.50 to the order
            if( (ChoiceIngredient == 2) ||
                (ChoiceIngredient == 3) ||
                (ChoiceIngredient == 4) )
                PriceIngredient = 0.50;
            else
                PriceIngredient = 0.00;
    
            // Instead of multiplying a number scoops to a value,
            // We will use an incremental value depending on 
            // the number of scoops
            if( Scoops == 1 )
                PriceScoop = 0.65;
            else if( Scoops == 2 )
                PriceScoop = 1.05;
            else
                PriceScoop = 1.55;
    
            // Calculate the total price of the ice scream
            TotalPrice = BasePrice + PriceScoop + PriceIngredient;
    
            // Create the ice scream...
    
            // And display a receipt to the user
            DisplayReceipt();
        }
    
        // This method is used to display a receipt to the user
        void DisplayReceipt()
        {
            Console::WriteLine(L"\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
            Console::WriteLine(L"Ice Scream Order");
            Console::WriteLine(L"-----------------------------------");
            Console::WriteLine(L"Flavor:      {0}",
                               Flavor[ChoiceFlavor - 1]);
            Console::WriteLine(L"Container:   {0}",
                               Container[ChoiceContainer - 1]);
            Console::WriteLine(L"Ingredient:  {0}",
                               Ingredient[ChoiceIngredient - 1]);
            Console::WriteLine(L"Scoops:      {0}", Scoops);
            Console::WriteLine(L"Total Price: {0:C}", TotalPrice);
            Console::WriteLine(L"=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
        }
    	
        void SaveOrder()
        {
            String ^ strNameOfFile;
                
            Console::Write(L"Please enter your initials or the name "
                           L"we will use to remember your order: ");
            strNameOfFile = Console::ReadLine();
            strNameOfFile = strNameOfFile + L".icr";
                
            // Find out if the user entered a name of a file 
            // that is already in the machine
    		if( File::Exists(strNameOfFile) )
            {
                char answer;
                    
                FileStream ^ stmIceCream =
    				gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceCream =
                        gcnew BinaryWriter(stmIceCream);
    
                // If so, find out if the user wants to 
                // replace the old file
                Console::WriteLine(L"The file you entered exists already.");
                Console::Write(L"Do you want to replace it(y/n)?");
    			answer = char::Parse(Console::ReadLine());
    
                // If the customer wants to replace it...
                if( (answer == L'y') || (answer == 'Y') )
                {
                    // ... do so
                    Console::WriteLine(L"The former order with the same "
                                       L"name will be replaced");
    
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                    bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceCream->Write(Container[ChoiceContainer - 1]);
                    bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceCream->Write(Scoops);
                    bnwIceCream->Write(TotalPrice);
                }
                // If the customer wants to save the new order with 
                // a different name
                else if( (answer == L'n') || (answer == 'N') )
                {
                    // Ask the user to enter a name to remember the order
                    Console::Write(L"Please enter a name we will use "
                                   L"to remember this order: ");
                    strNameOfFile = Console::ReadLine();
                    strNameOfFile = strNameOfFile + L".icr";
    
    				stmIceCream = gcnew FileStream(strNameOfFile, FileMode::Create);
                    bnwIceCream = gcnew BinaryWriter(stmIceCream);
    
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                    bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceCream->Write(Container[ChoiceContainer - 1]);
                    bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceCream->Write(Scoops);
                    bnwIceCream->Write(TotalPrice);
                }
                else
                    Console::WriteLine(L"Invalid Answer - We will close");
    
                bnwIceCream->Close();
                stmIceCream->Close();
            }
            else
            {
                FileStream ^ stmIceCream =
    				gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceCream =
                        gcnew BinaryWriter(stmIceCream);
    
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
    
                bnwIceCream->Write(Flavor[ChoiceFlavor - 1]);
                bnwIceCream->Write(Container[ChoiceContainer - 1]);
                bnwIceCream->Write(Ingredient[ChoiceIngredient - 1]);
                bnwIceCream->Write(Scoops);
                bnwIceCream->Write(TotalPrice);
    
                bnwIceCream->Close();
                stmIceCream->Close();
            }
        }
    
        void OpenOrder()
        {
            
                String ^ strNameOfFile;
    		
                String ^ SelectedFlavor;
                String ^ SelectedContainer;
                String ^ SelectedIngredient;
    
    	    // Ask the user to enter a name of a previously saved order
    	    Console::Write(L"Please enter the name you previously "
                           L"gave to remember your order: ");
    	    strNameOfFile = Console::ReadLine();
            strNameOfFile = strNameOfFile + L".icr";
    
    	    FileStream ^ stmIceCream =
    			gcnew FileStream(strNameOfFile, FileMode::Open);
    	    BinaryReader ^ bnrIceCream =
                    gcnew BinaryReader(stmIceCream);
                
            // Find out if this order was previously saved in the machine
    		if( File::Exists(strNameOfFile) )
            {
                // If so, open it
                SelectedFlavor = bnrIceCream->ReadString();
                SelectedContainer = bnrIceCream->ReadString();
                SelectedIngredient = bnrIceCream->ReadString();
                Scoops = bnrIceCream->ReadInt32();
                TotalPrice = bnrIceCream->ReadDouble();
                    
                // And display it to the user
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Previous Order: {0}", strNameOfFile);
                Console::WriteLine(L"Flavor:      {0}", SelectedFlavor);
                Console::WriteLine(L"Container:   {0}", SelectedContainer);
                Console::WriteLine(L"Ingredient:  {0}", SelectedIngredient);
                Console::WriteLine(L"Scoops:      {0}", Scoops);
                Console::WriteLine(L"Total Price: {0:C}\n", TotalPrice);
                    
                bnrIceCream->Close();
                stmIceCream->Close();
            }
            else
                Console::WriteLine(L"The name you entered is not "
                                   L"registered in our previous orders");
        }
    };
    
    int main(array<System::String ^> ^args)
    {
        String ^ answer = L"n";
        CIceCream ^ ic = gcnew CIceCream();
    
        Console::Write(L"Do you want to re-order a previously "
                       L"saved order(y/n)? ");
        answer = Console::ReadLine();
                
        if( (answer == L"y") || (answer == L"Y") )
            ic->OpenOrder();
        else
        {
            ic->ProcessAnOrder();
    
            Console::Write(L"Do you want us to remember this "
                           L"order the next time you come to "
                           L"get your ice scream (y/n)? ");
            answer = Console::ReadLine();
    
            if( (answer == L"y") || (answer == L"Y") )
                ic->SaveOrder();
            else
                Console::WriteLine(L"\nIt was nice serving you."
                                   L"\nCome Again!!!\n");
        }
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application and test it. Here is an example:
     
    Do you want to re-order a previously saved order(y/n)? Y
    Please enter the name you previously gave to remember your order: LS
    
    =-= Ice Scream Vending Machine =-=
     Previous Order: LS.icr
    Flavor:      Caramel Au Lait
    Container:   Cup
    Ingredient:  M & M
    Scoops:      2
    Total Price: $3.10
    
    Press any key to continue . . .
  3. Close the DOS window
  4. Execute the application again and test it. Here is an example:
     
    Do you want to re-order a previously saved order(y/n)? w
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Vending Machine
    -----------------------------------
    What type of flavor do you want?
    1 - Vanilla
    2 - Cream of Cocoa
    3 - Chocolate Chip
    4 - Organic Strawberry
    5 - Butter Pecan
    6 - Cherry Coke
    7 - Chocolate Brownies
    8 - Caramel Au Lait
    9 - Chunky Butter
    10 - Chocolate Cookie
    Your Choice? 5
    -----------------------------------
    What type of container do you want?
    1 - Cone
    2 - Cup
    3 - Bowl
    Your Choice? 2
    -----------------------------------
    Do you want an ingredient or not
    1 - No Ingredient
    2 - Peanuts
    3 - M & M
    4 - Cookies
    Your Choice? 1
    -----------------------------------
    How many scoops(1, 2, or 3)? 1
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Order
    -----------------------------------
    Flavor:      Butter Pecan
    Container:   Cup
    Ingredient:  No Ingredient
    Scoops:      1
    Total Price: $2.20
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time 
    you come to get your ice scream (y/n)? y
    Please enter your initials or the name we will use 
    to remember your order: DIC
    
    =-= Ice Scream Vending Machine =-=
     Saving Order: DIC.icr
    Press any key to continue . . .
  5. Close the DOS window
  6. Execute the application again and test it. Here is an example:
     
    Do you want to re-order a previously saved order(y/n)? n
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Vending Machine
    -----------------------------------
    What type of flavor do you want?
    1 - Vanilla
    2 - Cream of Cocoa
    3 - Chocolate Chip
    4 - Organic Strawberry
    5 - Butter Pecan
    6 - Cherry Coke
    7 - Chocolate Brownies
    8 - Caramel Au Lait
    9 - Chunky Butter
    10 - Chocolate Cookie
    Your Choice? 9
    -----------------------------------
    What type of container do you want?
    1 - Cone
    2 - Cup
    3 - Bowl
    Your Choice? 3
    -----------------------------------
    Do you want an ingredient or not
    1 - No Ingredient
    2 - Peanuts
    3 - M & M
    4 - Cookies
    Your Choice? 4
    -----------------------------------
    How many scoops(1, 2, or 3)? 3
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Order
    -----------------------------------
    Flavor:      Chunky Butter
    Container:   Bowl
    Ingredient:  Cookies
    Scoops:      3
    Total Price: $3.60
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time 
    you come to get your ice scream (y/n)? Y
    Please enter your initials or the name we will use 
    to remember your order: LS
    The file you entered exists already.
    Do you want to replace it(y/n)?Y
    The former order with the same name will be replaced
    
    =-= Ice Scream Vending Machine =-=
     Saving Order: LS.icr
    Press any key to continue . . .
  7. Close the DOS window
 

Previous Copyright © 2004-2016, FunctionX, Inc. Next