C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet503/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   499   500   501   502   503   504   505   506   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Using MemoryStream

Sometimes it is useful to read input from or to write output to an array, rather than directly 

from or to a device. To do this, you will use 

MemoryStream

.

MemoryStream

 is an 

implementation of 



Stream

 that uses an array of bytes for input and/or output. 



MemoryStream

 defines several constructors. Here is the one we will use:

MemoryStream(byte[ ] 

buf

)

www.freepdf-books.com




PART I

C h a p t e r   1 4 :  

U s i n g   I / O  

393


PART IPART I

Here, 


buf

 is an array of bytes that will be used for the source and/or target of I/O requests. 

The stream created by this constructor can be written or read, and supports 

Seek( )

. When 


using this constructor, you must remember to make 

buf

 large enough to hold whatever 

output you will be directing to it.

Here is a program that demonstrates the use of 



MemoryStream

:

// Demonstrate MemoryStream.



using System;

using System.IO;

class MemStrDemo {

  static void Main() {

    byte[] storage = new byte[255];

    // Create a memory-based stream.

    MemoryStream memstrm = new MemoryStream(storage);

    // Wrap memstrm in a reader and a writer.

    StreamWriter memwtr = new StreamWriter(memstrm);

    StreamReader memrdr = new StreamReader(memstrm);

    // Write to storage, through memwtr.

    for(int i=0; i < 10; i++)

       memwtr.WriteLine("byte [" + i + "]: " + i);

    // Put a period at the end.

    memwtr.WriteLine(".");

    memwtr.Flush();

    Console.WriteLine("Reading from storage directly: ");

    // Display contents of storage directly.

    foreach(char ch in storage) {

      if (ch == '.') break;

      Console.Write(ch);

    }


    Console.WriteLine("\nReading through memrdr: ");

    // Read from memstrm using the stream reader.

    memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer

    string str = memrdr.ReadLine();

    while(str != null) {

      str = memrdr.ReadLine();

      if(str.CompareTo(".") == 0) break;

      Console.WriteLine(str);

    }

  }


}

www.freepdf-books.com




394

 

P a r t   I :  



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

The output from the program is shown here:

Reading from storage directly:

byte [0]: 0

byte [1]: 1

byte [2]: 2

byte [3]: 3

byte [4]: 4

byte [5]: 5

byte [6]: 6

byte [7]: 7

byte [8]: 8

byte [9]: 9

Reading through memrdr:

byte [1]: 1

byte [2]: 2

byte [3]: 3

byte [4]: 4

byte [5]: 5

byte [6]: 6

byte [7]: 7

byte [8]: 8

byte [9]: 9

In the program, an array of bytes called 



storage

 is created. This array is then used as the 

underlying storage for a 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   499   500   501   502   503   504   505   506   ...   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