如何解决读取超过 2GB 的大文本文件时缓冲区溢出的问题?

发布于 2024-10-28 08:27:32 字数 443 浏览 1 评论 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 文件无法放入缓冲区。请帮助如何解决这个问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2024-11-04 08:27:32

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

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.

小女人ら 2024-11-04 08:27:32

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

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)

Loop over a line instead of reading the whole file

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文