C# 0 The Complete Reference


PART I C h a p t e r   1 4



Download 4,07 Mb.
Pdf ko'rish
bet499/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   495   496   497   498   499   500   501   502   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 4 :  

U s i n g   I / O  

389


PART IPART I

    Console.WriteLine();

    // Now, open inventory file for reading.

    try {

      dataIn = new

          BinaryReader(new FileStream("inventory.dat",

                       FileMode.Open));

    }


    catch(IOException exc) {

      Console.WriteLine("Cannot Open Inventory File For Input");

      Console.WriteLine(exc.Message);

      return;

    }

    // Lookup item entered by user.



    Console.Write("Enter item to lookup: ");

    string what = Console.ReadLine();

    Console.WriteLine();

    try {

      for(;;) {

        // Read an inventory entry.

        item = dataIn.ReadString();

        onhand = dataIn.ReadInt32();

        cost = dataIn.ReadDouble();

        // See if the item matches the one requested.

        // If so, display information.

        if(item.CompareTo(what) == 0) {

          Console.WriteLine(onhand + " " + item + " on hand. " +

                            "Cost: {0:C} each", cost);

          Console.WriteLine("Total value of {0}: {1:C}." ,

                            item, cost * onhand);

          break;

        }

      }

    }


    catch(EndOfStreamException) {

      Console.WriteLine("Item not found.");

    }

    catch(IOException exc) {



      Console.WriteLine("Error Reading Inventory File");

      Console.WriteLine(exc.Message);

    }

    dataIn.Close();



  }

}

Here is a sample run:



Enter item to look up: Screwdrivers

18 Screwdrivers on hand. Cost: $1.50 each

Total value of Screwdrivers: $27.00.

www.freepdf-books.com




390

 

P a r t   I :  



T h e   C #   L a n g u a g e

In the program, notice how inventory information is stored in its binary format. Thus, the 

number of items on hand and the cost is stored using their binary format rather than their 

human-readable text-based equivalents. This makes it is possible to perform computations 

on the numeric data without having to convert it from its human-readable form.

There is one other point of interest in the inventory program. Notice how the end of the 

file is detected. Since the binary input methods throw an 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   495   496   497   498   499   500   501   502   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish