| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1317 人关注过本帖
标题:请教关于通过查找函数查找文件中字符串的语句
只看楼主 加入收藏
ictest
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:333
专家分:114
注 册:2010-2-17
结帖率:70%
收藏
 问题点数:0 回复次数:1 
请教关于通过查找函数查找文件中字符串的语句
在网上找到一篇《VB快速查找大型文件中包含的字符串》的文章,简直就是拜读了一把,但还是云里雾里,请教通过这般文章里的源码,1、查找字符串所在的行数(第几行),2、以及该字符串所在的整行内容,这两个语句怎么写?求帮助。
附文章中的源码:
基于内存影射和模拟指针的查找函数,代码如下:
程序代码:
Option Explicit  
  
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)  
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long  
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long  
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long  
Private Const GENERIC_READ = &H80000000  
Private Const GENERIC_WRITE = &H40000000  
Private Const OPEN_EXISTING = 3  
Private Const FILE_SHARE_READ = &H1  
Private Const FILE_SHARE_WRITE = &H2  
Private Const FILE_ATTRIBUTE_NORMAL = &H80  
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20  
Private Const FILE_ATTRIBUTE_READONLY = &H1  
Private Const FILE_ATTRIBUTE_HIDDEN = &H2  
Private Const FILE_ATTRIBUTE_SYSTEM = &H4  
  
Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, ByVal lpFileMappigAttributes As Long, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long  
Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long  
Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long  
Private Const PAGE_READWRITE = &H4  
Private Const FILE_MAP_READ = &H4  
  
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long  
Private Type SAFEARRAYBOUND  
    cElements As Long  
    lLbound As Long  
End Type  
Private Type SAFEARRAY1D  
    cDims As Integer  
    fFeatures As Integer  
    cbElements As Long  
    clocks As Long  
    pvData As Long  
    rgsabound(0) As SAFEARRAYBOUND  
End Type  
  
'使用内存映射方式查找大型文件中包含的字符串  
Function FindTextInFile(ByVal strFileName As String, ByVal strText As String) As Long  
    Dim hFile As Long, hFileMap As Long  
    Dim nFileSize As Long, lpszFileText As Long, pbFileText() As Byte  
    Dim ppSA As Long, pSA As Long  
    Dim tagNewSA As SAFEARRAY1D, tagOldSA As SAFEARRAY1D  
  
    hFile = CreateFile(strFileName, _  
            GENERIC_READ Or GENERIC_WRITE, _  
            FILE_SHARE_READ Or FILE_SHARE_WRITE, _  
            0, _  
            OPEN_EXISTING, _  
            FILE_ATTRIBUTE_NORMAL Or FILE_ATTRIBUTE_ARCHIVE Or FILE_ATTRIBUTE_READONLY Or _  
            FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_SYSTEM, _  
            0) '打开文件  
    If hFile <> 0 Then  
        nFileSize = GetFileSize(hFile, ByVal 0&) '获得文件大小  
        hFileMap = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, 0, vbNullString) '创建文件映射对象  
        lpszFileText = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0) '将映射对象映射到进程内部的地址空间  
          
        ReDim pbFileText(0) '初始化数组  
        ppSA = VarPtrArray(pbFileText) '获得指向SAFEARRAY的指针的指针  
        CopyMemory pSA, ByVal ppSA, 4 '获得指向SAFEARRAY的指针  
        CopyMemory tagOldSA, ByVal pSA, Len(tagOldSA) '保存原来的SAFEARRAY成员信息  
        CopyMemory tagNewSA, tagOldSA, Len(tagNewSA) '复制SAFEARRAY成员信息  
        tagNewSA.rgsabound(0).cElements = nFileSize '修改数组元素个数  
        tagNewSA.pvData = lpszFileText '修改数组数据地址  
        CopyMemory ByVal pSA, tagNewSA, Len(tagNewSA) '将映射后的数据地址绑定至数组  
          
        FindTextInFile = InStr(pbFileText, StrConv(strText, vbFromUnicode)) '查找子字符串位置  
          
        CopyMemory ByVal pSA, tagOldSA, Len(tagOldSA) '恢复数组的SAFEARRAY结构成员信息  
        Erase pbFileText '删除数组  
          
        UnmapViewOfFile lpszFileText '取消地址映射  
        CloseHandle hFileMap '关闭文件映射对象的句柄  
    End If  
    CloseHandle hFile '关闭文件  
End Function  

    调用代码如下:

Sub Main()  
    Call FindTextInFile("G:/Inst/小说/沧海凤歌.txt", "打打秋风") '此返回值为字节位置  
End Sub  
搜索更多相关主题的帖子: 字符串 文章 网上 
2017-05-09 14:20
ictest
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:333
专家分:114
注 册:2010-2-17
收藏
得分:0 
求路过的前辈教教我,调用了上面的查找函数后,通过什么语句可以查看"打打秋风"这个字符串所在的行数,以及这个字符串所在行的所有内容?另外还想问个问题,如果"打打秋风"这个字符串在文中多次出现,如何得知这个字符串分别在哪里?
2017-05-09 16:59
快速回复:请教关于通过查找函数查找文件中字符串的语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029906 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved