首页
社区
课程
招聘
[求助]如何读取一个文本文件的特定内容?
2006-12-15 02:24 4513

[求助]如何读取一个文本文件的特定内容?

2006-12-15 02:24
4513
例如一个文件的内容是:

醒时饮酒醉时歌
诸君碌碌我蹉跎
牛衣空负眉头白
酒中不胜是呤哦
青蚨血尽无长物
心魔原来是酒魔
将进酒,将进酒
席上无歌方劝酒
无酒无歌且奈何
.
.
.

我现在想读取这个文件里,是否发现 “心魔原来是酒魔”这句诗,该怎么做,

下面是我写的主要代码:

szLogFile db "C:\test.txt",0
lpBuffer db '心魔原来是酒魔',0
sz1 db '成功找到!',0
x_str db 256 dup(0)

.data?
hFile dd ?

_File   proc
local   x_strlen: DWORD
invoke CreateFile, addr szLogFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL
mov hFile, eax
   invoke   SetFilePointer,hFile,0,0,FILE_BEGIN ;设定指针
   invoke   ReadFile ,hFile, addr  x_str ,8, addr  x_strlen , NULL ;读取文件
   invoke   lstrcmp , addr  x_str,addr lpBuffer ;对比是否发现指定诗句
.if eax != NULL
invoke MessageBox,NULL,addr sz1,NULL,MB_OK ;如果符合,就处理
.
.
_File endp

调式了很久还是不知道怎么写,所以就到看雪来问问


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
打赏
分享
最新回复 (7)
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
Winker 8 2006-12-15 19:41
2
0
没人指导吗?

我写了一些,调式都头晕了 

.386
.model flat,stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc

includelib kernel32.lib
includelib user32.lib

.data
FileName db "C:\test.txt",0
lpBuffer db '心魔原来是酒魔',0
sz1 db '成功找到!',0
x_str db 256 dup(0)
szjie db '结果是: %d',0
x_strlen DWORD 20
hSize dd ?
.code
start:
_ifile   proc
                    local   @hFile,@dwFileSize,@hMapFile,@lpMemory
                   invoke  CreateFile,addr FileName,GENERIC_READ,\
                            FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,\
                            OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
                    .if     eax !=  INVALID_HANDLE_VALUE
                            mov     @hFile,eax
                            invoke  GetFileSize,eax,NULL
                            mov     @dwFileSize,eax
                            .if     eax
                           invoke  CreateFileMapping,@hFile,\
                                    NULL,PAGE_READONLY,0,0,NULL
                            .if     eax
                                    mov     @hMapFile,eax
                                    invoke  MapViewOfFile,eax,\
                                            FILE_MAP_READ,0,0,0
                                    .if     eax
                                            mov     @lpMemory,eax
   invoke lstrlen,addr FileName
   mov hSize,eax
   invoke   ReadFile ,@lpMemory, addr  x_str ,hSize, addr  x_strlen , NULL
   invoke   lstrcmp , addr  lpBuffer,addr x_str
   .if eax != NULL
;这里开始处理找到的
    mov   eax  ,  FALSE
    ret
   .endif
          invoke  UnmapViewOfFile,@lpMemory
;********************************************************************
  .endif
   invoke  CloseHandle,@hMapFile
  .endif
   invoke  CloseHandle,@hFile
  .endif
   .endif   
   mov   eax , TRUE
   ret
_ifile   endp
call _ifile
invoke ExitProcess,NULL
end start
雪    币: 224
活跃值: (147)
能力值: ( LV9,RANK:970 )
在线值:
发帖
回帖
粉丝
wynney 24 2006-12-15 20:36
3
0
Delphi的源码要不要?
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
Winker 8 2006-12-15 21:10
4
0
发来看看...参考参考 
雪    币: 234
活跃值: (94)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
auser 2006-12-15 21:28
5
0
看不懂,既然MapViewOfFile了,为啥还要ReadFile ?
雪    币: 224
活跃值: (147)
能力值: ( LV9,RANK:970 )
在线值:
发帖
回帖
粉丝
wynney 24 2006-12-15 22:38
6
0
看看这个是不是你要的效果

先打开test.txt文本
再点button1

你要的那句就复制出来了:)
上传的附件:
雪    币: 297
活跃值: (10)
能力值: ( LV9,RANK:250 )
在线值:
发帖
回帖
粉丝
默数悲伤 6 2006-12-16 00:19
7
0
#include <windows.h>

int StrEqual(const char* deststr, const char *srcstr);
int FindText(LPVOID lpAddr, int memsize, LPCSTR lpKeyWord);
int FindSpecText(LPCSTR lpFileName, LPCSTR lpKeyWord);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
        if(0 <= FindSpecText("test.txt", "一个简单的字符串查找程序"))
                MessageBox(NULL, "Find it", "Test", 0);
        return 0;
}

int StrEqual(const char* deststr, const char *srcstr)
{
        int len = strlen(srcstr);
        for(int i = 0; i < len; i++)
        {
                if(deststr[i] != srcstr[i])
                        return -1;
        }
        return 0;
}

int FindText(LPVOID lpAddr, int memsize,  LPCSTR lpKeyWord)
{
        if(NULL == lpAddr || NULL == lpKeyWord || 0 == memsize)
                return -1;
        int len = strlen(lpKeyWord);
        for(int i = 0; i < memsize - len; i++)
        {
                if(0 == StrEqual((LPCSTR)lpAddr + i, lpKeyWord))        return i;
        }
        return -1;
}

int FindSpecText(LPCSTR lpFileName, LPCSTR lpKeyWord)
{
        int iret = 0;
        HANDLE hFile = INVALID_HANDLE_VALUE;
        HANDLE hMapFile = NULL;
        LPVOID lpMap = NULL;
        __try
        {
                hFile = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                if(INVALID_HANDLE_VALUE == hFile)        { iret = -1; __leave; }
                hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
                if(NULL == hMapFile)        { iret = -1; __leave; }
                lpMap = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
                if(NULL == lpMap)        { iret = -1; __leave; }
                iret = FindText(lpMap, GetFileSize(hFile, NULL), lpKeyWord);
        }
        __finally
        {
                if(lpMap != NULL)
                        UnmapViewOfFile(lpMap);
                if(hMapFile != NULL)
                        CloseHandle(hMapFile);
                if(hFile != INVALID_HANDLE_VALUE)
                        CloseHandle(hFile);
        }
       
        return iret;
}
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
Winker 8 2006-12-16 08:17
8
0
OK,已经搞定........我已经可以实现了,嘿嘿 
游客
登录 | 注册 方可回帖
返回