【发布时间】:2014-12-28 21:55:31
【问题描述】:
我有一些 C# 代码要调用为 TF(true,"C:\input.txt","C:\noexistsyet.file"),但是当我运行它时,它会在 FileStream.Read() 上中断,因为它会将文件的最后一块读入缓冲区,得到一个索引越界 @987654323 @。
对我来说,代码似乎合乎逻辑,尝试写入缓冲区时没有溢出。我以为我已经用rdlen 和_chunk 进行了所有设置,但也许我看错了。有什么帮助吗?
我的错误:ArgumentException was unhandled: 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.
public static bool TF(bool tf, string filepath, string output)
{
long _chunk = 16 * 1024; //buffer count
long total_size = 0
long rdlen = 0;
long wrlen = 0;
long full_chunks = 0;
long end_remain_buf_len = 0;
FileInfo fi = new FileInfo(filepath);
total_size = fi.Length;
full_chunks = total_size / _chunk;
end_remain_buf_len = total_size % _chunk;
fi = null;
FileStream fs = new FileStream(filepath, FileMode.Open);
FileStream fw = new FileStream(output, FileMode.Create);
for (long chunk_pass = 0; chunk_pass < full_chunks; chunk_pass++)
{
int chunk = (int)_chunk * ((tf) ? (1 / 3) : 3); //buffer count for xbuffer
byte[] buffer = new byte[_chunk];
byte[] xbuffer = new byte[(buffer.Length * ((tf) ? (1 / 3) : 3))];
//Read chunk of file into buffer
fs.Read(buffer, (int)rdlen, (int)_chunk); //ERROR occurs here
//xbuffer = do stuff to make it *3 longer or *(1/3) shorter;
//Write xbuffer into chunk of completed file
fw.Write(xbuffer, (int)wrlen, chunk);
//Keep track of location in file, for index/offset
rdlen += _chunk;
wrlen += chunk;
}
if (end_remain_buf_len > 0)
{
byte[] buffer = new byte[end_remain_buf_len];
byte[] xbuffer = new byte[(buffer.Length * ((tf) ? (1 / 3) : 3))];
fs.Read(buffer, (int)rdlen, (int)end_remain_buf_len); //error here too
//xbuffer = do stuff to make it *3 longer or *(1/3) shorter;
fw.Write(xbuffer, (int)wrlen, (int)end_remain_buf_len * ((tf) ? (1 / 3) : 3));
rdlen += end_remain_buf_len;
wrlen += chunk;
}
//Close opened files
fs.Close();
fw.Close();
return false; //no functionality yet lol
}
【问题讨论】:
-
Stream.Copy 或 File.Copy 怎么样?
-
学会爱
usingblocks。 -
您应该重新考虑方法/参数名称
TF/tf。谁会敢用tffalse 打电话给TF? -
@C.Evenhuis:代码就是一个例子。我保存的文件实际上并没有使用 TF(,,*) 调用,我只是将 TF 放在那里作为示例。与非功能返回 false/true 相同; @BenVoigt:我在下面看到了有用的代码
using,这可能是我的代码的第 5 次或第 6 次重写,因为我在代码中的其他文件不同时使用了using。并且@usr 复制功能我相信有自己的块大小,但我相信这对我来说并不重要,如果我需要,我将不得不考虑合并这些功能谢谢大家!
标签: c# arrays io byte filestream