在WHILE循环中处理C#filestream输入导致执行时错误

dig*_*ron 3 .net c# filestream

我有一个C#控制台应用程序,我正在尝试创建它处理给定目录中的所有文件并将输出写入另一个给定目录.我想一次处理输入文件X字节.

namespace FileConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcFolder = args[0];  
            string destFolder = args[1];   
            string[] srcFiles = Directory.GetFiles(srcFolder);
            for (int s = 0; s < srcFiles.Length; s++)
            {
                byte[] fileBuffer;
                int numBytesRead = 0;
                int readBuffer = 10000;
                FileStream srcStream = new FileStream(srcFiles[s], FileMode.Open, FileAccess.Read);
                int fileLength = (int)srcStream.Length;

                string destFile = destFolder + "\\" + Path.GetFileName(srcFiles[s]) + "-processed";
                FileStream destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);

                //Read and process the source file by some chunk of bytes at a time
                while (numBytesRead < fileLength)
                {
                    fileBuffer = new byte[readBuffer];

                    //Read some bytes into the fileBuffer
                    //TODO: This doesn't work on subsequent blocks
                    int n = srcStream.Read(fileBuffer, numBytesRead, readBuffer);

                    //If we didn't read anything, there's no more to process
                    if (n == 0)
                        break;

                    //Process the fileBuffer
                    for (int i = 0; i < fileBuffer.Length; i++)
                    {
                        //Process each byte in the array here
                    }
                    //Write data
                    destStream.Write(fileBuffer, numBytesRead, readBuffer);
                    numBytesRead += readBuffer;
                }
                srcStream.Close();
                destStream.Close();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在执行时遇到错误:

//Read some bytes into the fileBuffer
//TODO: This doesn't work on subsequent blocks
int n = srcStream.Read(fileBuffer, numBytesRead, readBuffer);
Run Code Online (Sandbox Code Playgroud)

我不想将整个文件加载到内存中,因为它的大小可能是几千兆字节.我真的希望能够读取一些字节数,处理它们,将它们写入文件,然后读入下一个X字节并重复.

它通过循环的一次迭代,然后在第二次迭代.我得到的错误是:

"偏移量和长度超出了数组的范围,或者计数大于从索引到源集合末尾的元素数量."

我正在使用的示例文件大约是32k.

谁能告诉我这里我做错了什么?

Mar*_*ell 8

Read的第二个参数不是文件的偏移量- 它是缓冲区中开始写入数据的偏移量.所以只需传递0.

此外,不要假设每次都填充缓冲区:您应该只处理缓冲区中的"n"个字节.并且应该在迭代之间重用缓冲区.

如果您需要准确读取多个字节:

static void ReadOrThrow(Stream source, byte[] buffer, int count) {
     int read, offset = 0;
     while(count > 0 && (read = source.Read(buffer, offset, count)) > 0) {
        offset += read;
        count -= read;
    }
    if(count != 0) throw new EndOfStreamException();
}
Run Code Online (Sandbox Code Playgroud)

请注意,Write的工作方式类似,因此您需要将0作为偏移量并将n作为计数.