0

我正在尝试在 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 文件无法放入缓冲区,因此出现缓冲区溢出。请帮助如何解决这个问题。

4

2 回答 2

1

读取文件的较小部分,检查字符串,清空缓冲区,阅读更多内容。只需确保在读取块时考虑字符串的长度,以防止切割它并因此错过命中。

于 2011-03-31T12:22:05.410 回答
0

循环遍历一行而不是读取整个文件

http://techsupt.winbatch.com/ts/T000001033F64.html

:test4
; read single chars from a line, count chars.
testfile = "d:\temp\test.ascii.txt"
MyFile = fso.OpenTextFile(testfile, ForReading)       ; Open a file as a TextStream
ThisLine = ""
While !MyFile.AtEndOfLine                             ; Is the current position at the end of a line? 
   ThisColumnCount = MyFile.Column                    ; Current column number.
   ThisLine        = StrCat(ThisLine, MyFile.Read(1)) ; Read a specific number of characters into a string.
   NextColumnCount = MyFile.Column                    ; Current column number.
EndWhile
MyFile.Close                                          ; Close a text stream.
ObjectClose(MyFile)

:test5
; read lines, count lines.
testfile = "d:\temp\test.ascii.txt"
MyFile = fso.OpenTextFile(testfile, ForReading)       ; Open a file as a TextStream
While !MyFile.AtEndOfStream                           ; Is the current position at the end of the stream?
   ThisLineCount = MyFile.Line                        ; Current line number.
   ThisLine      = MyFile.ReadLine                    ; Read an entire line into a string.
   NextLineCount = MyFile.Line                        ; Current line number.
EndWhile
MyFile.Close                                          ; Close a text stream.
ObjectClose(MyFile)
于 2011-03-31T12:24:33.687 回答