在while循环的处理C#中的文件流的输入导致执行时间错误 [英] Processing C# filestream input in WHILE loop causing execution time error

查看:135
本文介绍了在while循环的处理C#中的文件流的输入导致执行时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#控制台应用程序,我想创建一个进程在给定目录下的所有文件,并写入输出到另一个指定的目录。我想在同一时间处理输入文件X字节。

I have a C# console app that I'm trying to create that processes all the files in a given directory and writes output to another given directory. I want to process the input files X bytes at a time.

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();
            }
        }
    }
}

我遇到了一个错误,在执行时间:

I'm running into an error at execution time at:

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

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

I don't want to load the entire file into memory, as it could possibly be many gigabytes in size. I really want to be able to read some number of bytes, process them, write them out to a file, and then read in the next X bytes and repeat.

它获得通过循环的一个迭代,然后死在了第二位。我得到的错误是:

It gets through one iteration of the loop, and then dies on the second. The error I get is:

偏移和长度分别出界为阵列或计数大于从索引到源集合的端部部件的数量。

"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

我正与示例文件大约是32K。

The sample file I'm working with is about 32k.

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

Can anyone tell me what I'm doing wrong here?

推荐答案

第二个参数读的是不是偏移量的文件的 - 它的偏移量缓存在开始写入数据。所以只是通过0。

The second parameter to Read is not the offset into the file - it is the offset into the buffer at which to start writing data. So just pass 0.

另外,不要以为缓冲区每次填充:你应该只处理N从缓冲区的字节。和缓冲应迭代之间重复使用。

Also, don't assume the buffer is filled each time: you should only process "n" bytes from the buffer. And the buffer should be reused between iterations.

如果你需要阅读完全字节数:

If you need to read exactly a number of bytes:

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();
}

请注意,写工作方式类似,所以你需要传递0作为偏移,n为计数。

Note that Write works similarly, so you need to pass 0 as the offset and n as the count.

这篇关于在while循环的处理C#中的文件流的输入导致执行时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆