Thursday 15 January 2015

C# Filestream Read - Recycle array? -


I'm working with filestream Read:

What I'm trying to do is read Is a large file bytes in a certain number of loops at a time; One of the shows to not read the code examples entire file at a time:

  int n = fsSource.Read (bytes, numBytesRead, numBytesToRead);  

The "byte" definition: "When this method returns, offset and the specified byte array with the values ​​(replaced by reading offset + count - 1) bytes.

  using (FileStream fsInputFile = new FileStream (strInputFileName, FileMode.Open: current source " 

I have only one point of time I want to read 1 MB to do Are FileAccess. Read)) {int intBytesToRead = 1024; Int intTotalBytesRead = 0; Int intInputFileByteLength = 0; Byte [] BTINputBlock = new byte [intoto read]; Byte [] BTOutputBlock = new byte [intro to read]; IntInputFileByteLength = (int) fsInputFile.Length; While (intInputFileByteLength - 1 & gt; = intTotalBytesRead) {if (intInputFileByteLength - intTotalBytesRead & LT; intBytesToRead) {intBytesToRead = intInputFileByteLength - intTotalBytesRead; } // *** The problem here is *** integer n = fsInputFile.Read (btInputBlock, intTotalBytesRead, intBytesToRead); IntTotalBytesRead + = n; FsOutputFile.Write (btInputBlock, intTotalBytesRead - n, n); } FsOutputFile.Close (); }

Where the problem area is said, btInputBlock works on the first cycle because it reads in 1024 bytes. But then on the second loop, it does not recycle this byte array. Instead btInputBlock tries to add new 1024 bytes. As far as I can tell, I have only been dumped by can specify offset and length of the file you want to read, and Filmstrym that one way to "offset" to what Array length Bitiinputblok reuse Used to be. Read or should I get any other solution?

Thank you.

P. s. The exception to reading is: "Offset and length for oreset or counting was out of bounds, by the end of the source collection, more than the number of elements of the index."

That's because you increase the intTotalBytesRead, which is offset to the array, not Filestrym . In your case it should always be zero, which will overwrite the previous byte data in the array, instead of adding it to the end, using intTotalBytesRead.

  int n = fsInputFile.Read (btInputBlock, intTotalBytesRead, Torbha introbates); // currently int n = fsInputFile Read (btInputBlock, 0, intBytesToRead); // should be  

The filestream does not require an offset, it takes up every read, where the last left is


No comments:

Post a Comment