y_zyx
y_zyx

Reputation: 642

read from file in c#

The code

FileStream fs = new FileStream(fileName, FileMode.Open)
fs.ReadByte()

will read a byte from the file, what should I do to read 2 bytes at a time ?

Upvotes: 0

Views: 229

Answers (3)

Frank Razenberg
Frank Razenberg

Reputation: 531

Allocate a 2-byte array and pass that as argument to the FileStream.Read function.

byte[] twoBytes = new byte[2];
int bytesRead = fs.Read(twoBytes, 0, twoBytes.Length);

Upvotes: 4

Rhapsody
Rhapsody

Reputation: 6077

Use the 'normal' read method. Use the parameters to define the number of bytes you want to read.

Upvotes: 0

immutabl
immutabl

Reputation: 6903

Wrap the call up in a loop and read into a datastructure eg. byte[] ?

Upvotes: 0

Related Questions