Exception Handling in File Processing

Finally

So far, to handle exceptions, we were using the try, catch, and throw keywords. These allowed us to perform normal assignments in a try section and then handle an exception, if any, in a catch block. We also mentioned that, when you create a stream, the operating system must allocate resources and dedicate them to the file processing operations. Additional resources may be provided for the object that is in charge of writing to, or reading from, the stream. We also saw that, when the streaming was over, we should free the resources and give them back to the operating system. To do this, we called the Close() method of the variable that was using resources.

More than any other assignment, file processing is in prime need of exception handling. During file processing, there are many things that can go wrong. For this reason, the creation and/or management of streams should be performed in a try block to get ready to handle exceptions that would occur. Besides actually handling exceptions, you can use the finally keyword to free resources.

The finally keyword is used to create a section of an exception. Like catch, a finally block cannot exist by itself. It can be created following a try section. The formula used would be:

try
{
}
finally
{
}

Based on this:

Because the finally clause always gets executed, you can include any type of code in it but it is usually appropriate to free the resources that were allocated such as those used during streaming. Here is an example:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Members.clb";
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
        
    try 
    {
        wrtPersons->Write(L"James Bloch");
        wrtPersons->Write(L"Catherina Wallace");
        wrtPersons->Write(L"Bruce Lamont");
        wrtPersons->Write(L"Douglas Truth");
    }
    finally
    {
        wrtPersons->Close();
        fstPersons->Close();
    }

    return 0;
}

In the same way, you can use a finally section to free resources used when reading from a stream:

using namespace System;
using namespace System::IO;

int main()
{
/*    String ^ NameOfFile = L"Members.clb";
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
        
    try 
    {
        wrtPersons->Write(L"James Bloch");
        wrtPersons->Write(L"Catherina Wallace");
        wrtPersons->Write(L"Bruce Lamont");
        wrtPersons->Write(L"Douglas Truth");
    }
    finally
    {
        wrtPersons->Close();
        fstPersons->Close();
    }*/

	String ^ NameOfFile = L"Members.clb";
    String ^ strLine = nullptr;
        
    FileStream ^ fstMembers = gcnew FileStream(NameOfFile, FileMode::Open);
    BinaryReader ^ rdrMembers = gcnew BinaryReader(fstMembers);

    try
    {
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
    }
    finally
    {
        rdrMembers->Close();
        fstMembers->Close();
    }

    return 0;
}

Of course, since the whole block of code starts with a try section, it is used for exception handling. This means that you can add the necessary and appropriate catch section(s) but you don't have to.

Practical LearningPractical Learning: Finally Releasing Resources

  1. Start Microsoft Visual C++ and create a CLR Console Application named IceCream4
  2. To create a new class, on the main menu, click Project -> Add Class...
  3. In the Add Class dialog box, in the Templates list, click C++ Class and click Add
  4. Set the Name to CIceCream
  5. Make sure the Managed check box is selected and click Finish
  6. Change the file as follows:
    #pragma once
    
    using namespace System;
    using namespace System::IO;
    
    public ref class CIceCream sealed
    {
        // This is the base price of an ice scream
        // Optional values may be added to it
    public:
        static const double BasePrice = 1.55;
    
        // These arrays are used to build the components 
        // of various ice creams
    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(void);
    };
  7. Access the IceCream.cpp file and change the constructor as follows:
    #include "StdAfx.h"
    #include "IceScream.h"
    
    CIceCream::CIceCream(void)
    {
            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";
    }
  8. In the Class View, right-click CIceCream -> Add -> Add Function
  9. Set the Return Type to void
  10. Set the Function Name to ChooseFlavor and click Finish
  11. In the IceCream.cpp source file, implement the ChooseFlavor method as follows:
    // This method requests a flavor from the user and
    // returns the choice
    void CIceCream::ChooseFlavor(void)
    {
        // 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);
    }
  12. In the Class View, right-click CIceCream -> Add -> Add Function
  13. Set the Return Type to void
  14. Set the Function Name to ChooseContainer and click Finish
  15. In the IceCream.cpp source file, implement the ChooseContainer method as follows:
     
    void CIceCream::ChooseContainer(void)
    {
        // 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) );
    }
  16. In the Class View, right-click CIceCream -> Add -> Add Function
  17. Set the Return Type to void
  18. Set the Function Name to ChooseIngredient and click Finish
  19. In the IceCream.cpp source file, implement the ChooseIngredient method as follows:
    void CIceCream::ChooseIngredient(void)
    {
        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) );
    }
  20. In the Class View, right-click CIceCream -> Add -> Add Function
  21. Set the Return Type to void
  22. Set the Function Name to SpecifyNumberOfScoops and click Finish
  23. In the IceCream.cpp source file, implement the SpecifyNumberOfScoops method as follows:
    void CIceCream::SpecifyNumberOfScoops(void)
    {
        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);
    }
  24. In the Class View, right-click CIceCream -> Add -> Add Function
  25. Set the Return Type to void
  26. Set the Function Name to ProcessAnOrder and click Finish
  27. In the IceCream.cpp source file, implement the ProcessAnOrder method as follows:
    void CIceCream::ProcessAnOrder(void)
    {
        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();
    }
  28. In the Class View, right-click CIceCream -> Add -> Add Function
  29. Set the Return Type to void
  30. Set the Function Name to DisplayReceipt and click Finish
  31. In the IceCream.cpp source file, implement the DisplayReceipt method as follows:
    void CIceCream::DisplayReceipt(void)
    {
        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");
    }
  32. In the Class View, right-click CIceCream -> Add -> Add Function
  33. Set the Return Type to void
  34. Set the Function Name to SaveOrder and click Finish
  35. In the IceCream.cpp source file, implement the SaveOrder method as follows:
    void CIceCream::SaveOrder(void)
    {
        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) )
        {
            String ^ answer;
    
            FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
            BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
            try {
                // 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 = Console::ReadLine();
    
                // If the customer wants to replace it...
                if( (answer == L"y") || (answer == L"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);
                    bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceScream::Write(Container[ChoiceContainer - 1]);
                    bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceScream::Write(Scoops);
                    bnwIceScream::Write(TotalPrice);
                }
                // If the customer wants to save the new order with 
                // a different name
                else if( (answer == L"n") || answer == L"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";
    
                    stmIceScream = gcnew FileStream(strNameOfFile, FileMode::Create);
                    bnwIceScream = gcnew BinaryWriter(stmIceScream);
    
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                    bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceScream::Write(Container[ChoiceContainer - 1]);
                    bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceScream::Write(Scoops);
                    bnwIceScream::Write(TotalPrice);
                }
                else
                    Console::WriteLine(L"Invalid Answer - We will close");
            }
            finally
            {
                bnwIceScream->Close();
                stmIceScream->Close();
            }
        }
        else
        {
            FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
            BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
            try {
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                bnwIceScream::Write(Container[ChoiceContainer - 1]);
                bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                bnwIceScream::Write(Scoops);
                bnwIceScream::Write(TotalPrice);
            }
            finally
            {
                bnwIceScream->Close();
                stmIceScream->Close();
            }
        }
    }
  36. In the Class View, right-click CIceCream -> Add -> Add Function
  37. Set the Return Type to void
  38. Set the Function Name to OpenOrder and click Finish
  39. In the IceCream.cpp source file, implement the OpenOrder method as follows:
    void CIceCream::OpenOrder(void)
    {
        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 ^ stmIceScream =
                    gcnew FileStream(strNameOfFile, FileMode::Open);
        BinaryReader ^ bnrIceScream =
                    gcnew BinaryReader(stmIceScream);
    
        try {
            // Find out if this order was previously saved in the machine
    		if( File::Exists(strNameOfFile) )
            {
                // If so, open it
                SelectedFlavor = bnrIceScream->ReadString();
                SelectedContainer = bnrIceScream->ReadString();
                SelectedIngredient = bnrIceScream->ReadString();
                Scoops = bnrIceScream->ReadInt32();
                TotalPrice = bnrIceScream->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);
            }
            else
                Console::WriteLine(L"The name you entered is not "
                                   L"registered in our previous orders");
        }
        finally
        {
            bnrIceScream->Close();
            stmIceScream->Close();
        }  
    }
  40. Access the IceCream4.cpp source file and change it as follows:
     
    // IceCream4.cpp : main project file.
    
    #include "stdafx.h"
    #include "IceCream.h"
    
    using namespace System;
    
    int main()
    {
        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");
        }
    
        return 0;
    }
  41. Execute the application 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? 6
    -----------------------------------
    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)? 2
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Order
    -----------------------------------
    Flavor:      Cherry Coke
    Container:   Cup
    Ingredient:  No Ingredient
    Scoops:      2
    Total Price: $2.60
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time you 
    come to get your ice scream (y/n)? n
    
    It was nice serving you.
    Come Again!!!
    
    Press any key to continue . . .
  42. Close the DOS window
  43. Execute the application and test it. Enter a wrong file name. 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: dtr
    
    Unhandled Exception: System::IO.FileNotFoundException: Could not find file 'C:\Do
    cuments and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Vend
    ingMachine4\IceCream4\bin\Release\dtr.icr'.
    File name: 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2
    005\Projects\VendingMachine4\IceCream4\bin\Release\dtr.icr'
       at System::IO.__Error.WinIOError(Int32 errorCode, String ^ maybeFullPath)
       at System::IO.FileStream.Init(String ^ path, FileMode mode, FileAccess access, I
    nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
    ptions, SECURITY_ATTRIBUTES secAttrs, String ^ msgPath, Boolean bFromProxy)
       at System::IO.FileStream..ctor(String ^ path, FileMode mode)
       at IceCream4.IceScream.OpenOrder() in C:\Documents and Settings\Administrator
    \My Documents\Visual Studio 2005\Projects\VendingMachine4\IceCream4\IceScream.cs
    :line 360
       at IceCream4.Program.Main(String[] args) in C:\Documents and Settings\Adminis
    trator\My Documents\Visual Studio 2005\Projects\VendingMachine4\IceCream4\Progra
    m.cs:line 18
    Press any key to continue . . .
  44. Close the DOS window
  45. .NET Framework Exception Handling for File Processing

    In the previous lesson of our introduction to file processing, we behaved as if everything was alright. Unfortunately, file processing can be very strict in its assignments. Fortunately, the .NET Framework provides various Exception-oriented classes to deal with almost any type of exception you can think of.

    One of the most important aspects of file processing is the name of the file that will be dealt with. In some cases you can provide this name to the application or document. In some other cases, you would let the user specify the name of the path. Regardless of how the name of the file would be provided to the operating system, when this name is acted upon, the compiler will be asked to work on the file. If the file doesn't exist, the operation cannot be carried. Furthermore, the compiler would throw an error. There are many other exceptions that can be thrown as a result of something going bad during file processing:

    FileNotFoundException: The exception thrown when a file has not been found is of type FileNotFoundException. Here is an example of handling it:

    using namespace System;
    using namespace System::IO;
    
    int main()
    {
    /*    String ^ NameOfFile = L"Members.clb";
    	
        FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
        BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
            
        try 
        {
            wrtPersons->Write(L"James Bloch");
            wrtPersons->Write(L"Catherina Wallace");
            wrtPersons->Write(L"Bruce Lamont");
            wrtPersons->Write(L"Douglas Truth");
        }
        finally
        {
            wrtPersons->Close();
            fstPersons->Close();
        }*/
        
        String ^ NameOfFile = L"Members.clc";
        String ^ strLine = L"";
    
        try {
            FileStream ^ fstMembers =
                    gcnew FileStream(NameOfFile, FileMode::Open);
            BinaryReader ^ rdrMembers = gcnew BinaryReader(fstMembers);
    
            try {
                strLine = rdrMembers->ReadString();
                Console::WriteLine(strLine);
    			strLine = rdrMembers->ReadString();
                Console::WriteLine(strLine);
                strLine = rdrMembers->ReadString();
                Console::WriteLine(strLine);
                strLine = rdrMembers->ReadString();
                Console::WriteLine(strLine);
            }
            finally
            {
                rdrMembers->Close();
                fstMembers->Close();
            }
        }
        catch (FileNotFoundException ^ ex)
        {
            Console::Write(L"Error: " + ex->Message);
            Console::WriteLine(L" May be the file doesn't exist " +
                               L"or you typed it wrong!");
        }
    
        return 0;
    }

    Here is an example of what this would produce:

    Error: Could not find file 'c:\Documents and Settings\Administrator
    \My Documents\Visual Studio 2005\Projects\FileProcessing2
    \FileProcessing2\Members.clc'. May be the file doesn't exist 
    or you typed it wrong!
    Press any key to continue . . .

    IOException: As mentioned already, during file processing, anything could go wrong. If you don't know what caused an error, you can throw the IOException exception.

    Practical LearningPractical Learning: Handling File Processing Exceptions

    1. In the Scopes combo box, select CIceCream
    2. In the Functions combo box, select SaveOrder
    3. To throw exceptions, change the method as follows:
      void CIceCream::SaveOrder(void)
      {
          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";
      
          try {
              // Find out if the user entered a name of a file 
              // that is already in the machine
              if( File::Exists(strNameOfFile) )
              {
                  String ^ answer;
                  FileStream ^ stmIceScream =
                          gcnew FileStream(strNameOfFile, FileMode::Create);
                  BinaryWriter ^ bnwIceScream =
                          gcnew BinaryWriter(stmIceScream);
      
                  try {
                      // 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 = Console::ReadLine();
      
                      // If the customer wants to replace it...
                      if( (answer == "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);
                          bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                          bnwIceScream->Write(Container[ChoiceContainer - 1]);
                          bnwIceScream->Write(Ingredient[ChoiceIngredient - 1]);
                          bnwIceScream->Write(Scoops);
                          bnwIceScream->Write(TotalPrice);
                      }
                      // If the customer wants to save the new order with 
                      // a different name
                      else if( (answer == "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";
      
                         try {
                              stmIceScream = gcnew FileStream(strNameOfFile, FileMode::Create);
                              bnwIceScream = gcnew BinaryWriter(stmIceScream);
      
                              Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                              Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                              bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                              bnwIceScream->Write(Container[ChoiceContainer - 1]);
                              bnwIceScream->Write(Ingredient[ChoiceIngredient - 1]);
                              bnwIceScream->Write(Scoops);
                              bnwIceScream->Write(TotalPrice);
                          }
                          catch(IOException ^)
                          {
                              Console::Write(L"\nThe file you wanted us to "
      				       L"create exists already. ");
                              Console::WriteLine(L"In case it was registered "
                        			   L"by a different customer, "
      				           L"we will not delete it.");
                          }
                      }
                      else
                          Console::WriteLine(L"Invalid Answer - We will close");
                  }
                  catch(IOException ^)
                  {
                      Console::Write(L"\nThat file exists already. ");
                      Console::WriteLine(L"We need to preserve it just in "
      				   L"case another customer will require it.");
                  }
                  finally
                  {
                      bnwIceScream->Close();
                      stmIceScream->Close();
                  }
              }
              else
              {
                  FileStream ^ stmIceScream =
                          gcnew FileStream(strNameOfFile, FileMode::Create);
                  BinaryWriter ^ bnwIceScream =
                          gcnew BinaryWriter(stmIceScream);
      
                  try {
                      Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                      Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                      bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                      bnwIceScream->Write(Container[ChoiceContainer - 1]);
                      bnwIceScream->Write(Ingredient[ChoiceIngredient - 1]);
                      bnwIceScream->Write(Scoops);
                      bnwIceScream->Write(TotalPrice);
                  }
                  finally
                  {
                      bnwIceScream->Close();
                      stmIceScream->Close();
                  }
              }
          }
          catch(IOException ^ ex)
          {
              Console::WriteLine(L"\nError: " + ex->Message);
              Console::WriteLine(L"Operation Canceled: The file you want "
                                 L"to create exists already.");
          }
      }
    4. In the Functions combo box, select OpenOrder
    5. To throw exceptions, change the method as follows:
      void CIceCream::OpenOrder(void)
      {
          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 + ".icr";
      
          try {
              FileStream ^ stmIceScream =
                      gcnew FileStream(strNameOfFile, FileMode::Open);
              BinaryReader ^ bnrIceScream =
                      gcnew BinaryReader(stmIceScream);
      
              try {
                  // Find out if this order was previously saved in the machine
                  if( File::Exists(strNameOfFile) )
                  {
                      // If so, open it
                      SelectedFlavor = bnrIceScream->ReadString();
                      SelectedContainer = bnrIceScream->ReadString();
                      SelectedIngredient = bnrIceScream->ReadString();
                      Scoops = bnrIceScream->ReadInt32();
                      TotalPrice = bnrIceScream->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);
                  }
                  else
                      Console::WriteLine(L"The name you entered is not "
                                         L"registered in our previous orders");
              }
              finally
              {
                  bnrIceScream->Close();
                  stmIceScream->Close();
              }
          } 
          catch(FileNotFoundException ^ ex)
          {
              Console::Write(L"\nError: " + ex->Message);
              Console::WriteLine(L"It is unlikely that the file exists in "
                        		   L"our machine's register.");
          }
      }
    6. Exercise the application and test it
    7. Close the DOS window

    File Information

     

    Introduction

    In its high level of support for file processing, the .NET Framework provides the FileInfo class. This class is equipped to handle all types of file-related operations including creating, copying, moving, renaming, or deleting a file. FileInfo is based on the FileSystemInfo class that provides information on characteristics of a file.

    Practical Learning Practical Learning: Introducing File Information

    1. Start a new CLR Console Application named WattsALoan2
    2. To take advantage of the Visual Basic rich library, in the Solution Explorer, right-click WattsALoan2 and click References...
    3. In the Property Pages, click the Add New Reference button
    4. In the .NET tab, click Microsoft.VisualBasic
       
      Adding a Visual Basic Assembly
    5. Click OK and click OK
    6. Change the source file file as follows:
      // WattsALoan2.cpp : main project file.
      
      #include "stdafx.h"
      
      using namespace System;
      
      int main(array<System::String ^> ^args)
      {
          String ^ EmployerName, ^ ApplicantName;
          String ^ HomePhone, ^ WorkPhone;
          double LoanAmount = 0.00, InterestRate = 0.0;
          double MonthlyPayment = 0.00, Periods = 0;
      
          Console::WriteLine(L" -=- Car Loan Application -=-");
          Console::WriteLine(L"Enter the following pieces "
                             L"of information\n");
          Console::WriteLine(L"Applicant Information");
          Console::Write(L"Full Name:     ");
          ApplicantName = Console::ReadLine();
          Console::Write(L"Employer Name: ");
          EmployerName = Console::ReadLine();
          Console::Write(L"Home Phone:    ");
          HomePhone = Console::ReadLine();
          Console::Write(L"Work Phone:    ");
          WorkPhone = Console::ReadLine();
          Console::WriteLine(L"Loan Estimation");
      
          try {
              Console::Write(L"Amount of Loan: ");
      		LoanAmount = double::Parse(Console::ReadLine());
          }
          catch(FormatException ^)
          {
              Console::WriteLine(L"Invalid Loan Amount");
          }
      
          try {
              Console::Write(L"Interest Rate(0 to 100): ");
      		InterestRate = double::Parse(Console::ReadLine()) / 100;
          }
          catch(FormatException ^)
          {
              Console::WriteLine(L"Invalid Interest Rate");
          }
      
          try {
              Console::Write(L"Number of Months: ");
      		Periods = double::Parse(Console::ReadLine());
          }
          catch(FormatException ^)
          {
              Console::WriteLine(L"Invalid Number of Months");
          }
      
      	try {
              MonthlyPayment =
      	    Microsoft::VisualBasic::Financial::Pmt(InterestRate / 12,
                      Periods, -LoanAmount,
      		0, Microsoft::VisualBasic::DueDate::BegOfPeriod);
          }
          catch(ArgumentException ^)
          {
              Console::WriteLine(L"Some invalid values were provided\n");
          }
      
          Console::WriteLine();
          Console::WriteLine(L" -=-=-=-=-=-=-=-=-=-=-=-=-=-=");
          Console::WriteLine(L" -=- Car Loan Application -=-");
          Console::WriteLine(L" -=-=-=-=-=-=-=-=-=-=-=-=-=-=");
          Console::WriteLine(L"Applicant Information");
          Console::WriteLine(L"Full Name:     {0}", ApplicantName);
          Console::WriteLine(L"Employer Name: {0}", EmployerName);
          Console::WriteLine(L"Home Phone:    {0}", HomePhone);
          Console::WriteLine(L"Work Phone:    {0}", WorkPhone);
          Console::WriteLine(L"Loan Estimation");
          Console::WriteLine(L"Loan Amount:      {0:C}", LoanAmount);
          Console::WriteLine(L"Interest Rate:    {0:P}", InterestRate);
          Console::WriteLine(L"Number of Months: {0:F}", Periods);
          Console::WriteLine(L"Monthly Payment:  {0:C}\n", MonthlyPayment);
      
          return 0;
      }
    7. Execute the application and test it. Here is an example:
       
       -=- Car Loan Application -=-
      Enter the following pieces of information
      
      Applicant Information
      Full Name:     James Watts
      Employer Name: Wattson Enterprises
      Home Phone:    (202) 374-4738
      Work Phone:    (301) 894-4789
      Loan Estimation
      Amount of Loan: 12500
      Interest Rate(0 to 100): 10.25
      Number of Months: 48
      
       -=-=-=-=-=-=-=-=-=-=-=-=-=-=
       -=- Car Loan Application -=-
       -=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Applicant Information
      Full Name:     James Watts
      Employer Name: Wattson Enterprises
      Home Phone:    (202) 374-4738
      Work Phone:    (301) 894-4789
      Loan Estimation
      Loan Amount:      $12,500.00
      Interest Rate:    10.25 %
      Number of Months: 48.00
      Monthly Payment:  $315.84
      Press any key to continue...
    8. Close the DOS window
    9. File Initialization

      The FileInfo class is equipped with one constructor whose syntax is:

      public:
          FileInfo(String ^ fileName);

      This constructor takes as argument the name of a file or its complete path. If you provide only the name of the file, the compiler would consider the same directory of its project. Here is an example:

      FileInfo ^ fleMembers = gcnew FileInfo(L"First.txt");

      Alternatively, if you want, you can provide any valid directory you have access to. In this case, you should provide the complete path.

      File Creation

      The FileInfo constructor is mostly meant only to indicate that you want to use a file, whether it exists already or it would be created. Based on this, if you execute an application that has only a FileInfo object created using the constructor as done above, nothing would happen.

      To create a file, you have various alternatives. If you want to create one without writing anything in it, which implies creating an empty file, you can call the FileInfo::Create() method. Its syntax is:

      public:
          FileStream ^ Create();

      This method simply creates an empty file. Here is an example of calling it:

      FileInfo ^ fleMembers = gcnew FileInfo(L"First.txt");
      fleMembers->Create();

      The FileInfo::Create() method returns a FileStream object. You can use this returned value to write any type of value into the file, including text. If you want to create a file that contains text, an alternative is to call the FileInfo::CreateText() method. Its syntax is:

      public:
          StreamWriter CreateText();

      This method directly returns a StreamWriter object. You can use this returned object to write text to the file.

      File Existence

      When you call the FileInfo::Create() or the FileInfo::CreateText() method, if the file passed as argument, or as the file in the path of the argument, exists already, it would be deleted and a new one would be created with the same name. This can cause the right file to be deleted. Therefore, before creating a file, you may need to check whether it exists already. To do this, you can check the value of the Boolean FileInfo::Exists property. This property holds a true value if the file exists already and it holds a false value if the file doesn't exist or it doesn't exist in the path.

      Here is an example of checking the existence of a file:

      FileInfo ^ fleMembers = gcnew FileInfo(L"First.txt");
      fleMembers->Create();
      
      if( fleMembers.Exists == true )
      	return;
       

      Writing to a File

      As mentioned earlier, the FileInfo::Create() method returns a FileStream object. You can use this to specify the type of operation that would be allowed on the file.

      To write normal text to a file, you can first call the FileInfo::CreateText() method. This method returns a StreamWriter object. The StreamWriter class is based on the TextWriter class that is equipped with the Write() and the WriteLine() methods used to write values to a file. The Write() method writes text on a line and keeps the caret on the same line. The WriteLine() method writes a line of text and moves the caret to the next line.

      After writing to a file, you should close the StreamWriter object to free the resources it was using during its operation(s).

      Practical Learning Practical Learning: Writing to a File

      1. To allow the user to create a new employee, change the file as follows:
         
        // WattsALoan2.cpp : main project file.
        
        #include "stdafx.h"
        
        using namespace System;
        using namespace System::IO;
        
        void CreateNewEmployee()
        {
            String ^ employeeNumber, ^ employeeName;
            FileInfo ^ fleEmployees = gcnew FileInfo(L"Employees.txt");
            StreamWriter ^ swrEmployees = fleEmployees->CreateText();
        
            try {
                Console::WriteLine(L"Hiring New Employee");
                Console::Write(L"Enter Employee Number as 00-000: ");
                employeeNumber = Console::ReadLine();
                Console::Write(L"Enter Employee Name: ");
                employeeName = Console::ReadLine();
        
                swrEmployees->WriteLine(employeeNumber);
                swrEmployees->WriteLine(employeeName);
        
           }
           finally
           {
               swrEmployees->Flush();
               swrEmployees->Close();
           }
        }
        
        int main(array<System::String ^> ^args)
        {
            try {
                Console::Write(L"Do you want to hire a new "
                               L"employee(0=No/1=Yes)? ");
                int answer = int::Parse(Console::ReadLine());
        
                if( answer == 1 )
                    CreateNewEmployee();
            }
            catch(FormatException ^)
            {
                Console::WriteLine(L"That was an invalid answer!\n");
            }
        
            String ^ EmployerName, ^ ApplicantName;
            String ^ HomePhone, ^ WorkPhone;
            double LoanAmount = 0.00, InterestRate = 0.0;
            double MonthlyPayment = 0.00, Periods = 0;
        
            . . . No Change
        
            return 0;
        }
      2. Execute the application. Here is an example:
         
        Do you want to hire a new employee(0=No/1=Yes)? 1
        Hiring New Employee
        Enter Employee Number as 00-000: 44-228
        Enter Employee Name: Johnny Olney
      3. Close the DOS window

      Appending to a File

      You may have created a text-based file and written to it. If you open such a file and find out that a piece of information is missing, you can add that information to the end of the file. To do this, you can call the FileInfo::AppenText() method. Its syntax is:

      public:
          StreamWriter ^ AppendText();

      When calling this method, you can retrieve the StreamWriter object that it returns, then use that object to add new information to the file.

      Practical Learning Practical Learning: Appending to a File

      1. To allow the user to add more employees to the file that holds their names, change the file as follows:
         
        // WattsALoan2.cpp : main project file.
        
        #include "stdafx.h"
        
        using namespace System;
        using namespace System::IO;
        
        void CreateNewEmployee()
        {
            String ^ employeeNumber, ^ employeeName;
            FileInfo ^ fleEmployees = gcnew FileInfo(L"Employees.txt");
            StreamWriter ^ swrEmployees = nullptr;
        
            // If the file exists already, then add the new employee to it
            if( fleEmployees->Exists == true)
                swrEmployees = fleEmployees->AppendText();
            else // Otherwise, create a new file
                swrEmployees = fleEmployees->CreateText();
        
            try {
                . . . No Change
        
           }
           finally
           {
               swrEmployees->Flush();
               swrEmployees->Close();
           }
        }
        
        int main(array<System::String ^> ^args)
        {
            . . . No Change
        
            return 0;
        }
      2. Execute the application
      3. Type y to create an employee. Here is an example:
         
        Do you want to hire a new employee(0=No/1=Yes)? 1
        Hiring New Employee
        Enter Employee Number as 00-000: 72-604
        Enter Employee Name: Ernest Barzan
      4. Close the DOS window

      Previous Copyright © 2004-2025, FunctionX Monday 14 October 2024, 10:12 Next