如何解决读取超过 2GB 的大文本文件时缓冲区溢出的问题?
我试图在 JScript 中读取一个大文本文件来搜索字符串,但遇到溢出异常。我写了下面的代码。
var ForReading = 1;
var TriStateFalse = 0;
var strFileData;
var fso, objFile, objTS;
fso = new ActiveXObject("Scripting.FileSystemObject");
objFile = fso.GetFile("Sample2GBFile");
strFileData = objTS.Read(objFile.Size);
if(strFileData .indexOf("String to search") > 0 )
{
wShShell.Echo("Found...");
}
在上面的代码中,我遇到了缓冲区溢出,因为 2GB 文件无法放入缓冲区。请帮助如何解决这个问题。
I am trying to read a large text file in JScript to search for a string, but encountering overflow exception. I wrote the following code.
var ForReading = 1;
var TriStateFalse = 0;
var strFileData;
var fso, objFile, objTS;
fso = new ActiveXObject("Scripting.FileSystemObject");
objFile = fso.GetFile("Sample2GBFile");
strFileData = objTS.Read(objFile.Size);
if(strFileData .indexOf("String to search") > 0 )
{
wShShell.Echo("Found...");
}
In the above code, I am getting the buffer overflow as the 2GB file is unable to fit into the buffer. pls help how to resolve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
读取文件的较小部分,检查字符串、空缓冲区,然后读取更多内容。只需确保在读取块时考虑到字符串的长度,以防止切割它并因此错过命中。
Read smaller portions of the file, check for string, empty buffer, read more. Just make sure you account for the length of the string when reading a chunk so as to prevent cutting it and thus missing a hit.
循环一行而不是读取整个文件
http://techsupt.winbatch.com/ts/T000001033F64 .html
Loop over a line instead of reading the whole file
http://techsupt.winbatch.com/ts/T000001033F64.html